C# Xml 移除指定节点

XML样例:<wbr><div> <pre class="prettyprint"><p></p><pre code_snippet_id="147351" snippet_file_name="blog_20140108_1_6593790" name="code" class="html">&lt;?xml version="1.0" encoding="gb2312"?&gt; &lt;bookstore&gt; &lt;book genre="李1" ISBN="2-3645-4"&gt; &lt;title&gt;Net从入门到精通&lt;/title&gt; &lt;author&gt;李大蒜&lt;/author&gt; &lt;price&gt;58.3&lt;/price&gt; &lt;/book&gt; &lt;book genre="李2" ISBN="2-3631-4"&gt; &lt;title&gt;CS从入门到精通&lt;/title&gt; &lt;author&gt;候捷&lt;/author&gt; &lt;price&gt;58.3&lt;/price&gt; &lt;/book&gt; &lt;book genre="李3" ISBN="2-3631-4"&gt; &lt;title&gt;CS从入门到精通&lt;/title&gt; &lt;author&gt;候捷&lt;/author&gt; &lt;price&gt;58.3&lt;/price&gt; &lt;/book&gt; &lt;/bookstore&gt;</pre><p></p></pre> 执行代码一:</div> <div> <pre class="prettyprint"><p></p><pre code_snippet_id="147351" snippet_file_name="blog_20140108_2_672992" name="code" class="csharp"> /// &lt;summary&gt; /// 删除属性值等于“AttributeValue”的节点 /// &lt;/summary&gt; /// &lt;param name="xmlFileName"&gt;XML文档完全文件名(包含物理路径)&lt;/param&gt; /// &lt;param name="xpath"&gt;要匹配的XPath表达式(例如:"//节点名//子节点名&lt;/param&gt; /// &lt;param name="xmlAttributeName"&gt;要删除包含xmlAttributeName属性的节点的名称&lt;/param&gt; /// &lt;param name="AttributeValue"&gt;&lt;/param&gt; private void XmlNodeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string AttributeValue) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlNodeList xNodes = xmlDoc.SelectSingleNode(xpath).ChildNodes; for (int i = xNodes.Count - 1; i &gt;= 0; i--) { XmlElement xe = (XmlElement)xNodes[i]; if (xe.GetAttribute(xmlAttributeName) == AttributeValue) { xNodes[i].ParentNode.RemoveChild(xNodes[i]); } } xmlDoc.Save(xmlFileName); }</pre><p></p></pre> 实验:XmlNodeByXPath("E:\\bookstore.xml", "bookstore", "genre", "李3");</div> <div>结果:</div> <div> <pre class="prettyprint"><p></p><pre code_snippet_id="147351" snippet_file_name="blog_20140108_3_6703257" name="code" class="html">&lt;?xml version="1.0" encoding="gb2312"?&gt; &lt;bookstore&gt; &lt;book genre="李1" ISBN="2-3645-4"&gt; &lt;title&gt;Net从入门到精通&lt;/title&gt; &lt;author&gt;李大蒜&lt;/author&gt; &lt;price&gt;58.3&lt;/price&gt; &lt;/book&gt; &lt;book genre="李2" ISBN="2-3631-4"&gt; &lt;title&gt;CS从入门到精通&lt;/title&gt; &lt;author&gt;候捷&lt;/author&gt; &lt;price&gt;58.3&lt;/price&gt; &lt;/book&gt; &lt;/bookstore&gt;</pre><p></p></pre> 小注:</div> <div> <span style="white-space:pre"></span>1、删除节点不能使用foreach,使用的话会造成删除XML一个节点,就跳出循环,也不报错,很隐蔽的错误。</div> <div> <span style="white-space:pre"></span>2、该函数也可以这么实现</div> <div> <pre class="prettyprint"><pre code_snippet_id="147351" snippet_file_name="blog_20140108_4_956400" name="code" class="csharp"> <span style="white-space:pre"> </span>/// &lt;summary&gt; /// 删除属性值等于“AttributeValue”的节点 /// &lt;/summary&gt; /// &lt;param name="xmlFileName"&gt;XML文档完全文件名(包含物理路径)&lt;/param&gt; /// &lt;param name="xpath"&gt;要匹配的XPath表达式(例如:"//节点名//子节点名&lt;/param&gt; /// &lt;param name="xmlAttributeName"&gt;要删除包含xmlAttributeName属性的节点的名称&lt;/param&gt; /// &lt;param name="AttributeValue"&gt;&lt;/param&gt; private void XmlNodeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string AttributeValue) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlNode root = xmlDoc.SelectSingleNode(xpath); XmlNodeList xnl = xmlDoc.SelectSingleNode(xpath).ChildNodes; for (int i = 0; i &lt; xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute(xmlAttributeName) == AttributeValue) { root.RemoveChild(xe); if (i &lt; xnl.Count) i = i - 1; } } xmlDoc.Save(xmlFileName); }</pre></pre> <br> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值