Vignesh Kumar

Vignesh Kumar

  • NA
  • 1k
  • 415.9k

Remove element from XML based on condition

Oct 24 2016 2:03 AM
Hi,
 

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.

 

Answers (1)