The following fragment deletes “DpsRecord” elements which contain “Name” element which value contains “JOSE”. However, I don’t understand how after applying Linq query to root and getting dpsRecords ( left side of query ) and then removing in dpsRecords, elements are also removed in root.
XElement root = XElement.Load("input.xml");
IEnumerable<XElement> dpsRecords = from elem in
root.Elements("DpsRecord")
where (((string)elem.Element("Name")).Contains("JOSE"))
select elem;
foreach (XElement elem in dpsRecords)
{
elem.Remove();
}
root.Save("output.xml");
Well the Remove method http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.remove.aspx removes the node it is called on from its parent node.
Also take note that you don’t need the foreach, you can do
dspRecords.Remove().