0

Below is the XML structure I have.

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>

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 Details parent node. For Ex: Let's assume strDate a string that 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.

 object[] arr = xDos.Descendants("Details").Elements("EndDate").
                Select(element => element.Value).ToArray();

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

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

Really appreciate any suggestions on this.

Update 1:

 XDocument xDos = XDocument.Load(Server.MapPath("~/Info.xml"));
 XElement xDo = XElement.Load(Server.MapPath("~/Info.xml"));
 List<XElement> arr = xDo.Descendants("Details").Where(n => DateTime.ParseExact(n.Element("EndDate").Value + "", "dd-MM-yyyy", CultureInfo.InvariantCulture) < DateTime.Today.Date).ToList<XElement>();
            for (int i = 0; i < arr.Count(); i++)
            {
                XElement ele = arr[i];
                ele.Remove();
            }               
            xDos.Save(Server.MapPath("~/Info.xml"));
Learner
  • 353
  • 9
  • 37
  • loop the arr which you got above and remove like this, @Learner just give a try. doc.Descendants().Where(e => (string) e < "23-10-2016").Remove(); – Meena Oct 24 '16 at 05:37
  • @Meena - I tried this and it doesn't work. – Learner Oct 24 '16 at 05:56
  • then have a look at this http://stackoverflow.com/questions/30896485/remove-the-parent-node-based-on-child-condition – Meena Oct 24 '16 at 05:57
  • List arr = xDos.Descendants("Details").Where(n => DateTime.ParseExact(n.Element("EndDate").Value + "", "dd-MM-yyyy", CultureInfo.InvariantCulture) > DateTime.Today.Date).ToList(); This will give you a list... then run following for (int i = 0 ; i – A3006 Oct 24 '16 at 06:01
  • @Anand - It loops in and doesn't remove the values and moreover need to get less than todays date and it has to be lesser than.Any thoughts? – Learner Oct 24 '16 at 06:12
  • @Learner try this XDocument xd = XDocument.Parse(xml); xd.Element("Info").Element("Details").Elements("EndDate") .Where(x => (string)x != "23-10-2016") .Remove(); – Meena Oct 24 '16 at 06:13
  • @Learner - You can change the sign to get it working... It removed elements for me ... – A3006 Oct 24 '16 at 06:18
  • @Meena - This will work only for equality what s/he is interested in is; leas than/ greater than – A3006 Oct 24 '16 at 06:19
  • List arr = xDos.Descendants("Details").Where(n => DateTime.ParseExact(n.Element("EndDate").Value + "", "dd-MM-yyyy", CultureInfo.InvariantCulture) < DateTime.Today.Date).ToList(); This will give you a list... then run following for (int i = 0 ; i – A3006 Oct 24 '16 at 06:19
  • @Anand - I have updated my post. Can you pls take a look? – Learner Oct 24 '16 at 06:53
  • xDos.Descendants("Details").ToList() .ForEach(x => { if (DateTime.ParseExact(x.Element("EndDate").Value, "dd-MM-yyyy", CultureInfo.InvariantCulture) < DateTime.Today.Date) x.Remove(); }); – A3006 Oct 24 '16 at 09:43
  • This should work... – A3006 Oct 24 '16 at 09:44
  • @Anand - This worked. Can you post this as an answer, so I can mark it and upvote. – Learner Oct 25 '16 at 07:23

0 Answers0