What I want
retrieve task whch “due” element is overdue.
What the XML is
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<tasks>
<task>
<title>11111</title>
<due>2012/06/18</due>
</task>
<task>
<title>2121211212</title>
<due></due>
</task>
</tasks>
What I code
var res = from q in xml.Root.Descendants("task")
where q.Element("due").IsEmpty == false & (Convert.ToDateTime(q.Element("due").Value)).Date < DateTime.Now.Date
select q
What the error is
An exception of type ‘System.FormatException’ occurred in mscorlib.dll but was not handled in user code
Additional information: String was not recognized as a valid DateTime.
If there is a handler for this exception, the program may be safely continued.
-_-
If I delete the task which element”due” is empty, the error went away.
but Don’t I just filter the empty element with the code below?!
q.Element("due").IsEmpty == false
Why and how to solve it?
Per
XElement.IsEmptyYou could use
string.IsNullOrWhiteSpaceto check whether the element contains text characters: