以下片段删除包含“Name”元素的“DpsRecord”元素,该元素的值包含“JOSE”。但是,我不明白在将Linq查询应用于root并获取dpsRecords(查询的左侧)然后在dpsRecords中删除之后,如何在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");
答案 0 :(得分:2)
删除方法http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.remove.aspx删除了从其父节点调用的节点。
另请注意,您不需要foreach,您可以dspRecords.Remove()
。