How C # XML removes a specified node

Source: Internet
Author: User
Tags count expression xpath

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); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.