XML样例:<wbr><div>
<pre class="prettyprint"><p></p><pre code_snippet_id="147351" snippet_file_name="blog_20140108_1_6593790" name="code" class="html"><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="李1" ISBN="2-3645-4">
<title>Net从入门到精通</title>
<author>李大蒜</author>
<price>58.3</price>
</book>
<book genre="李2" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
<book genre="李3" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore></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"> /// <summary>
/// 删除属性值等于“AttributeValue”的节点
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlAttributeName">要删除包含xmlAttributeName属性的节点的名称</param>
/// <param name="AttributeValue"></param>
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 >= 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"><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="李1" ISBN="2-3645-4">
<title>Net从入门到精通</title>
<author>李大蒜</author>
<price>58.3</price>
</book>
<book genre="李2" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore></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>/// <summary>
/// 删除属性值等于“AttributeValue”的节点
/// </summary>
/// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>
/// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
/// <param name="xmlAttributeName">要删除包含xmlAttributeName属性的节点的名称</param>
/// <param name="AttributeValue"></param>
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 < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe.GetAttribute(xmlAttributeName) == AttributeValue)
{
root.RemoveChild(xe);
if (i < xnl.Count) i = i - 1;
}
}
xmlDoc.Save(xmlFileName);
}</pre></pre>
<br>
</div>
</wbr>
C# Xml 移除指定节点
最新推荐文章于 2023-05-15 14:30:00 发布
本文介绍了一个C#示例程序,演示如何通过XPath表达式查找并删除XML文档中特定属性值的节点。示例中详细展示了如何加载XML文件、定位节点、删除指定属性值的节点,并保存更改。
3556

被折叠的 条评论
为什么被折叠?



