Search code examples
c#.netxmllinq-to-xmlxdoc

XDocument Filter by attribute date value


I have the following XML structure, I need to filter it using LINQ by attribute "date" where the value of the "date" attribute is less than today.

The attribute date is on the format "yyyymmddhhmmss +Zone".

For example in the given XML first node date="20200318123000 +0000" means:

year=2020,

month=03,

day=18,

hours=12,

minutes=30,

seconds=00 &

timezone = UTC +0000.

<?xml version="1.0" encoding="utf-8"?>
<books>
 <book date="20200318123000 +0000">
   <length units="pages">270</length>
   <title>Book 1 Title</title>
   <category>Book 1 Category</category>
   <desc>Book 1 Description</desc>
 </book>
 <book date="20200319123000 +0000">
   <length units="pages">144</length>
   <title>Book 2 Title</title>
   <category>Book 2 Category</category>
   <desc>Book 2 Description</desc>
 </book>
</books>

I have tried doing it using the below code but it returns nothing in "IEnumerable elements" instead of filtered nodes.

  XDocument xDocument = XDocument.Load(fileName);

  DateTime t;

  IEnumerable<XElement> elements = xDocument.Descendants("book")
  .Where(d => d.NodeType == XmlNodeType.Attribute && d.Name.Equals("date") && 
  DateTime.TryParse(d.ToString().Split('+').First().Trim(), out t) && t < 
  DateTime.Today)
  .ToList();

Solution

  • Try following :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
    
                Dictionary<DateTime, List<XElement>> dict = doc.Descendants("book")
                    .GroupBy(x => DateTime.ParseExact((string)x.Attribute("date"),"yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.InvariantCulture), y => y)
                    .ToDictionary(x => x.Key, x => x.ToList());
    
                List<KeyValuePair<DateTime, XElement>> beforeToday = dict.Where(x => x.Key < DateTime.Now.Date).SelectMany(x => x.Value.Select(y => new KeyValuePair<DateTime, XElement>(x.Key, y))).ToList(); 
            }
        }
    }