XML Sample:
<?xml version= "1.0" encoding= "gb2312"?>
<bookstore>
<book genre= "Li 1" isbn= "2-3645-4" >
<title>net from getting started to mastering </title>
<author> li garlic </author>
<price>58.3</price >
</book>
<book genre= "Li 2" isbn= "2-3631-4" >
<title>cs from Getting started to mastering </title>
<author> Hou Jie </author>
<price>58.3</price>
</book>
<book genre= "Li 3 "isbn=" 2-3631-4 ">
<title>cs from Getting started to mastering </title>
<author> Hou Jie </author>
< price>58.3</price>
</book>
</bookstore>
Execute code One:
<summary>///Delete a node with a property value equal to "AttributeValue"///</summary>///<param name= "xmlfilename" >xml document completely File name (contains physical path) </param>///<param name= "XPath" > XPath expression to match (for example: "//Node naming//Sub-section name </param>///<param Name= "Xmlattributename" > to delete the name of the node containing the Xmlattributename property </param>///See more highlights in this column: http://www.bianceng.cn/ programming/csharp////<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); }
Experiment: Xmlnodebyxpath ("E:\\bookstore.xml", "bookstore", "genre", "Li 3");
Results:
<?xml version= "1.0" encoding= "gb2312"?>
<bookstore>
<book genre= "Li 1" isbn= "2-3645-4" >
<title>net from getting started to mastering </title>
<author> li garlic </author>
<price>58.3</price >
</book>
<book genre= "Li 2" isbn= "2-3631-4" >
<title>cs from Getting started to mastering </title>
<author> Hou Jie </author>
<price>58.3</price>
</book>
</bookstore >
Note
1, delete nodes can not use foreach, the use of words will result in the deletion of XML node, jump out of the loop, also do not complain, very covert errors.
2, this function can also be implemented
<span style= "White-space:pre" > </span>///<summary>///Delete a node with a property value equal to "AttributeValue"/ </summary>///<param name= "XMLfileName" >xml document full file name (including physical path) </param>///<param N Ame= "XPath" > XPath expression to match (for example: "//Node name//Sub-section naming </param>///<param name=" Xmlattributename "> to delete include Xmlattrib The name of the node for the Utename property </param>///<param name= "AttributeValue" ></param> private void XmlNode Byxpath (String XMLfileName, String xpath, String xmlattributename, String attributevalue) {Xmldocum
Ent 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); }