c# - 如果内部标记与 C# 中的 Linq 值匹配,则删除 XML 节点

标签 c# xml linq

<column>
    <format>boolicon</format>
    <heading>Attachment</heading>
    <prop>urn:schemas:httpmail:hasattachment</prop>
    <type>boolean</type>
    <bitmap>1</bitmap>
    <width>10</width>
    <style>padding-left:3px;text-align:center</style>
    <editable>0</editable>
    <displayformat>3</displayformat>
</column>
<column>
    <heading>From</heading>
    <prop>urn:schemas:httpmail:fromname</prop>
    <type>string</type>
    <width>68</width>
    <style>padding-left:3px;text-align:left</style>
    <editable>0</editable>
    <displayformat>1</displayformat>
</column>
<column>
    <heading>Subject</heading>
    <prop>urn:schemas:httpmail:subject</prop>
    <type>string</type>
    <width>326</width>
    <style>padding-left:3px;text-align:left</style>
    <editable>1</editable>
</column>

如果标题等于“主题”,我想删除列节点

(from node in xdoc.Descendants("column") select node).ToList().ForEach(x=>x.Remove());

仅当标题与“主题”匹配时才尝试选择节点。

提前致谢。

最佳答案

我认为,一个简单的 XPath 可以帮助您

xdoc.XPathSelectElement("//column[heading='Subject']").Remove();

PS:不要忘记包含 using System.Xml.XPath;

关于c# - 如果内部标记与 C# 中的 Linq 值匹配,则删除 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26187286/

相关文章:

c# - 在 Winium.Cruciatus 中找到你的主窗口元素

c# - 如何从 Word 编辑器对象(在 Outlook 2010 中)获取所选文本并将其复制到另一个表单?

css - XSL 无法用 Firefox 解析

c# - LINQ to SQL,检查与连接字符串的连接是否有效

c# - 参数数量未知的动态Where子句

c# - 为什么我的 OpenGL ES 应用程序在 glDrawElements 上崩溃?

c# - 除了 native UUID 之外,数据库中 UUID 最有效的数据类型

xml - 如果使用 xslt 的 xml 中不存在子节点,则删除父节点

c# - 在 C# 中更改由 XmlSerializer 生成的 XML 结构

c# - 如何在for循环中生成尽可能多的for循环?