0

Working on an issue where I can't seem to step through the elements and convert the date value as a datetime from the xml then print out all elements where the date is greater than "x" date to the console. The XML file is rather large, but here is a sample.

<DataSet>
   <diffgr:diffgram xmls="">
       <NewDataSet>
          <USER_LOGIN>
              <USER_LOGIN_ID>123</USER_LOGIN_ID>
              <DATE>2017-03-01T16:56:16.59-06:00</<DATE>
          </USER_LOGIN>
          <CONTENT>
              <CONTENT_ID>123</CONTENT_ID>
              <DATE>2017-03-01T16:56:16.59-06:00</<DATE>
          </CONTENT>
              ETC. ETC.
       <NewDataSet>
   </diffgr:diffgram> 
<DataSet>

In my code I have a list which I am wanting to read through and output all elements inside where date is greater than 'some hardcoded date'. I realize I can have a foreach loop for every element inside the descendant of but I want this to be dynamic because there are many more elements this must loop through than in my sample. Here is my current code. This fails and shows Null exception on the where clause in my code. If I remove the where clause it prints all elements in .

XDocument doc= XDocument.Load("xmlfile.xml");
DateTime testDate= new DateTime(2017, 03, 01);

IEnumerable<XElement> textSeg=
      from element in doc.Descendants("NewDataSet")
      where (DateTime)element.Element("DATE")>testDate
      select element;

      foreach (XElement element in textSeg)
         {Console.Writeline(element);}
  • Possible duplicate of [this](http://stackoverflow.com/questions/4570185/how-to-query-with-the-datetime-value-using-linq-to-xml) SO question. – Jeroen Heier Apr 20 '17 at 18:58
  • It seems probably you have some nodes without DATE elements? Try querying your data to find `where element.Element("DATE") == null` or add the `DateTime` cast and see what nodes have issues. – NetMage Apr 20 '17 at 23:48

1 Answers1

1

You are attempting to get DATE elements from the USER_LOGIN element and CONTENT elements directly; you need to visit their child elements.

(please excuse my use of method syntax over query syntax, this is what I prefer)

gist here

var dateElements = doc.Descendants("NewDataSet")

// gather all the child-elements of all NewDataSet elements
.SelectMany(dataSetEle => dataSetEle.Elements())

// filter out child-elements that themselves have no applicable DATE child-elements
.Where(childEle => childEle.Elements()
    .Any(grandChildEle =>
        grandChildEle.Name == "DATE" && (DateTime) grandChildEle > testDate)
);
John McDowall
  • 435
  • 3
  • 10
  • Thanks for the response. This prints back out only the DATE elements, but I want the output to print all other elements within the blocks as well. So I would like to print it back out in this format if the date>testdate. ** 123 2017-03-01T16:56:16.59-06:00 123 2017-03-01T16:56:16.59-06:00 ETC. ETC. ** – user7897499 Apr 21 '17 at 14:53
  • I've updated my answer to return the elements that contain DATE elements, rather than the DATE element itself. – John McDowall Apr 22 '17 at 06:58