Parse date time string with ascii character to datetime


I have datetime data getting in XML like this

<Start_Date>1/10/2023 5&#58;00&#58;00 PM</Start_Date>

How to convert this to C# datetime.

            string dateString_string = "1/10/2023 5&amp;#58;00&amp;#58;00 PM";
            CultureInfo culture = new CultureInfo("en-US");
            CultureInfo provider = CultureInfo.InvariantCulture;
            var tt = Convert.ToDateTime(dateString_string, culture);
            var tt1 = DateTime.Parse(dateString_string);

            DateTime dateTime10 = DateTime.ParseExact(dateString_string, "mm/dd/yyyy hh:mm:ss T", provider);

            DateTime dateTime12;
            bool isSuccess2 = DateTime.TryParse(dateString_string, out dateTime12);

            DateTime dateTime14;
            bool isSuccess6 = DateTime.TryParseExact(dateString_string, new string[] { "mm/dd/yyyy H&amp;#58;mm&amp;#58;ss tt"}, provider, DateTimeStyles.None, out dateTime14);

I try all this a no luck ..
any suggestion ?

>Solution :

You should first decode your string and then do the parsing.

string dateString = "1/10/2023 5&#58;00&#58;00 PM";
string decodedDate = WebUtility.HtmlDecode(dateString);
bool isSuccess = DateTime.TryParse(decodedDate, out DateTime parsedDate);

Leave a ReplyCancel reply