LINQ到XML递归删除元素-C#
我有以下XML:LINQ到XML递归删除元素-C#,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有以下XML: <Root> <Section name="xyz" /> <Section name="abc"> <Section name="def" /> </Section> <Section name="abc"> <Section name="def"> <Section name="xyz" /> <Section name="abc"
<Root>
<Section name="xyz" />
<Section name="abc">
<Section name="def" />
</Section>
<Section name="abc">
<Section name="def">
<Section name="xyz" />
<Section name="abc" />
<Section name="xyz">
<Section name="xyz" />
</Section>
</Section>
</Section>
</Root>
我有XML的XDocument
表示。如何遍历树并使用sayabc
删除所有元素,这非常简单:)
我一定很喜欢LINQ to XML
编辑:我刚刚用您的示例XML进行了尝试,结果如下:
<Root>
<Section name="abc">
<Section name="def" />
</Section>
<Section name="abc">
<Section name="def">
<Section name="abc" />
</Section>
</Section>
</Root>
如果这不是您所期望的,请告诉我。当Jon Skeet回答时,我甚至不需要验证:)谢谢Jon!顺便说一下,我假设我也可以在连接条件中使用
子体
?类似于var j=from h in doc.substands(“Section”)的东西在..
@Nick:绝对可以。它只返回一个IEnumerable
。
<Root>
<Section name="abc">
<Section name="def" />
</Section>
<Section name="abc">
<Section name="def">
<Section name="abc" />
</Section>
</Section>
</Root>