Click here to Skip to main content
16,019,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Below is the XML structure I have.

XML
XML
<Info>
<Details>
<ID>user</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-10-2016</EndDate>
</Details>
<Details>
<ID>user1</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>24-10-2016</EndDate>
</Details>
<Details>
<ID>user2</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-11-2016</EndDate>
</Details>
</Info>


C#
Now I need to compare the EndDate node with today's date. If EndDate is lesser than today's date then I need to remove the whole contents of Details. For Ex: Let's assume strDate a string has today's date.

The below code takes all the values of EndDate and now how can I compare the string value and loop through xml elements and delete them.



C#
Now this whole portion should be deleted as of comparing with todays date.

XML
<Details>
<ID>user</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-10-2016</EndDate>
</Details>


Really appreciate any suggestions on this.

What I have tried:

object[] arr = xDos.Descendants("Details").Elements("EndDate").
Select(element = element.Value).ToArray();
Posted
Updated 23-Oct-16 20:02pm
v2

This solution will will work but could fail if the XML either does not contain the EndDate node or the EndDate node is not a valid Date (I have created it as a Unit Test) [This could be mitigated by some form of XSD validation of the XML where the EndDate is a Mandatory Date]

C#
public void TestMethod1()
        {
            var todaysDate1 = new DateTime(2001, 10, 30); // no nodes selected
            var todaysDate2 = new DateTime(2016, 10, 28); // one node selected
            var todaysDate3 = new DateTime(2016, 10, 30); // 2 nodes selected
            var todaysDate4 = new DateTime(2016, 12, 25); // 3 nodes selected

            var xmlData ="<Info><Details><ID>user</ID><StartDate>23-10-2016</StartDate><EndDate>22-10-2016</EndDate></Details><Details><ID>user1</ID><StartDate>23-10-2016</StartDate><EndDate>29-10-2016</EndDate></Details><Details><ID>user2</ID><StartDate>23-10-2016</StartDate><EndDate>22-11-2016</EndDate></Details></Info>";
            XDocument xdoc = XDocument.Parse(xmlData);

            var nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate1) > 0).Remove();

            Assert.AreEqual(0, xdoc.Descendants("Details").Count());

            // Test 2
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate2) > 0).Remove();
            Assert.AreEqual(1, xdoc.Descendants("Details").Count());

            // Test 3
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate3) > 0).Remove();
            Assert.AreEqual(2, xdoc.Descendants("Details").Count());

            // Test 4
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate4) > 0).Remove();
            Assert.AreEqual(3, xdoc.Descendants("Details").Count());
        }
 
Share this answer
 
The issue of the Null exception bothered me - but thanks to Resharper here is an alternative (I have not been able to resolved the invalid date issue)

C#
nodes.Where(x =>
                {
                    var element = x.Element("EndDate");
                    return element != null && DateTime.Compare(DateTime.Parse(element.Value), todaysDate1) > 0;
                }).Remove();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900