Why is this value not a valid DateTime object?

I am new to XPath and I have the following problem.

I am working on a C # class that reads an XML file and uses the contents of its fields, sets the properties of an object.

In this class, I have the following instruction:

currentDeepSightVuln.Published = n_alertdocument.SelectSingleNode("./x:Published", nsmgr) == null ? DateTime.MinValue : DateTime.Parse(n_alertdocument.SelectSingleNode("./x:Published", nsmgr).InnerText);

This command sets the Published property of the currentDeepSightVuln object using the following field in the XML file:

<x:Published>Mar 11 2014</x:Published>

So, using the previous value ( March 11 2014 ), my program selected the following exception:

ex {"The string was not recognized as a valid DateTime because the day week was incorrect." } System.Exception {System.FormatException}

11 2014 19 2011 ( XML , , 19 2011 ) , .

11 2014 DateTime?

, ?

+4
2

:

DateTime.ParseExact("MAR 11 2014", "MMM dd yyyy", CultureInfo.InvariantCulture);
+2

- /. (/ .NET Framework) .

, -, , "" "", . "" "", .

, , / , , , .

XML , : "11 2014 ".

, , / - / ? , vvv , . :

string pattern = "MMM dd yyyy";
DateTime parsedDate;

if (DateTime.TryParseExact(dateValue, pattern, null, DateTimeStyles.None, out parsedDate))
{
    Console.WriteLine("Converted '{0}' to {1:d}.", dateValue, parsedDate);
}
+2

Source: https://habr.com/ru/post/1543052/


All Articles