繁体   English   中英

LINQ to XML递归删除元素-C#

[英]LINQ to XML recursively remove elements - C#

我有以下XML:

<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表示形式。 我如何遍历树并使用abc删除所有元素

真的很简单:)

doc.Descendants("Section")
   .Where(x => (string) x.Attribute("name") == "xyz")
   .Remove();

一定喜欢LINQ to XML ...

编辑:我刚刚用您的示例XML尝试过,这是之后的结果:

<Root>
  <Section name="abc">
    <Section name="def" />
  </Section>
  <Section name="abc">
    <Section name="def">
      <Section name="abc" />
    </Section>
  </Section>
</Root>

如果那不是您所期望的,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM