[英]Remove XML node if the inner tag matches value using Linq in C#
<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:不要忘记包含使用System.Xml.XPath;
您可以添加简单的where
子句来仅过滤具有子<heading>
等于"Subject"
<column>
元素:
(from node in xdoc.Descendants("column")
where (string)node.Element("heading") == "Subject"
select node
).Remove();
或者使用方法语法:
xdoc.Descendants("column")
.Where(n => (string)n.Element("heading") == "Subject")
.Remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.