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"));