C # xml Operation Summary 2: read, insert, modify, and delete

Source: Internet
Author: User

C #: XML Operation Summary 2: read, insert, modify, and delete

1. Read the value of an attribute in the node.

/// <Summary> /// read the value of an attribute in a node. If attribute is empty, the innertext of the entire node is returned, otherwise, return the value of the specific attribute /// </Summary> /// <Param name = "path"> XML file path </param> /// <Param name = "Node "> node </param> /// <Param name =" attribute "> attributes of a node </param> /// <returns> If attribute is empty, the innertext of the entire node is returned; otherwise, the value of the specific attribute is returned </returns> // use instance: xmlhelper. read (path, "personf/person [@ name = 'person2']", ""); // xmlhelper. read (path, "personf/person [@ name = 'person2']", "name"); Public Static string read (string path, string node, string attribute) {string value = ""; try {xmldocument Doc = new xmldocument (); Doc. load (PATH); xmlnode xn = Doc. selectsinglenode (node); value = (attribute. equals ("")? XN. innertext: Xn. attributes [attribute]. Value);} catch (exception e) {console. writeline (E. Message) ;}return value ;}

2. Add node elements and attributes to the node.

/// <Summary> /// Add a node element to the node, properties /// </Summary> /// <Param name = "path"> path </param> /// <Param name = "Node"> node to be operated </param> // <Param name = "element"> the node element to be added, it can be empty. Insert a new element if it is not empty. Otherwise, insert the attribute of the element </param> // <Param name = "attribute"> to add the node attribute. It can be empty or empty. Insert the element value if it is not null, otherwise, insert the element value </param> /// <Param name = "value"> value of the node to be added </param> /// use the instance xmlhelper. insert (path, "personf/person [@ name = 'person2']", "num", "ID", "88"); // xmlhelper. insert (path, "personf/person [@ name = 'person2']", "num", "", "88"); // xmlhelper. insert (path, "personf/person [@ name = 'person2']", "", "ID", "88"); public static void insert (string path, string node, string element, string attribute, string Value) {try {xmldocument Doc = new xmldocument (); Doc. load (PATH); xmlnode xn = Doc. selectsinglenode (node); // If the element is used, this attribute if (string. isnullorempty (element) {// If attribute is not empty, add this attribute if (! String. isnullorempty (attribute) {xmlelement Xe = (xmlelement) xn; // <person name = "person2" id = "88"> xmlhelper. insert (path, "personf/person [@ name = 'person2']", "num", "ID", "88"); Xe. setattribute (attribute, value) ;}} else // If the element is not empty, add the node {xmlelement Xe = Doc under preson. createelement (element); If (string. isnullorempty (attribute) // <person> <num> 88 </num> </person> xmlhelper. insert (path, "personf/person [@ name = 'person2']", "num", "", "88"); Xe. innertext = value; else // <person> <num id = "88"/> </person> xmlhelper. insert (path, "personf/person [@ name = 'person2']", "", "ID", "88"); Xe. setattribute (attribute, value); XN. appendchild (xe);} Doc. save (PATH);} catch (exception e) {console. writeline (E. message );}}

3. Modify node values

/// <Summary> /// modify the node value /// </Summary> /// <Param name = "path"> path </param> /// <Param name = "Node"> the node to be modified </param> // <Param name = "attribute"> attribute name, if it is not empty, modify the node attribute value. Otherwise, modify the node value </param> /// <Param name = "value"> attribute value </param> // xmlhelper instance. update (path, "personf/person [@ name = 'person3']/ID", "", "888"); // xmlhelper. update (path, "personf/person [@ name = 'person3']/ID", "num", "999"); public static void Update (string path, string node, string attribute, string value) {try {xmldocument Doc = new xmldocument (); Doc. load (PATH); xmlnode xn = Doc. selectsinglenode (node); xmlelement Xe = (xmlelement) xn; If (string. isnullorempty (attribute) Xe. innertext = value; // The original <ID> 2 </ID> is changed: <ID> 888 </ID> xmlhelper. update (path, "personf/person [@ name = 'person3']/ID", "", "888"); else Xe. setattribute (attribute, value); // The original <ID num = "3"> 888 </ID> variable <ID num = "999"> 888 </ID> xmlhelper. update (path, "personf/person [@ name = 'person3']/ID", "num", "999"); Doc. save (PATH);} catch (exception e) {console. writeline (E. message );}}

  

4. delete data

/// <Summary> /// delete data /// </Summary> /// <Param name = "path"> path </param> /// <Param name = "Node"> the node to be deleted </param> // <Param name = "attribute"> attribute, if it is null, the entire node is deleted. If it is not null, the attributes of the node are deleted </param> // instance: xmlhelper. delete (path, "personf/person [@ name = 'person3']/ID", ""); // xmlhelper. delete (path, "personf/person [@ name = 'person3']/ID", "num"); public static void Delete (string path, string node, string attribute) {try {xmldocument Doc = new xmldocument (); Doc. load (PATH); xmlnode xn = Doc. selectsinglenode (node); xmlelement Xe = (xmlelement) xn; If (string. isnullorempty (attribute) xn. parentnode. removechild (Xn); // The entire node of <ID num = "999"> 888 </ID> will be removed from xmlhelper. delete (path, "personf/person [@ name = 'person3']/ID", ""); else Xe. removeattribute (attribute); // <ID num = "999"> 888 </ID> to <ID> 888 </ID> xmlhelper. delete (path, "personf/person [@ name = 'person3']/ID", "num"); Doc. save (PATH);} catch (exception e) {console. writeline (E. message );}}

  

5. Call Method

      public static void XMLMTest()        {            string path = "http://www.cnblogs.com/../Person.xml";            XMLHelper.Read(path, "PersonF/person[@Name='Person2']", "Name");            XMLHelper.Insert(path, "PersonF/person[@Name='Person2']", "Num", "ID", "88");            XMLHelper.Insert(path, "PersonF/person[@Name='Person2']", "Num", "", "88");            XMLHelper.Insert(path, "PersonF/person[@Name='Person2']", "", "ID", "88");            XMLHelper.Update(path, "PersonF/person[@Name='Person3']", "Num", "888");            XMLHelper.Update(path, "PersonF/person[@Name='Person3']/ID", "Num", "999");            XMLHelper.Update(path, "PersonF/person[@Name='Person3']/ID", "", "888");            XMLHelper.Delete(path, "PersonF/person[@Name='Person3']/ID", "Num");            XMLHelper.Delete(path, "PersonF/person[@Name='Person3']/ID", "");        }

  

6. xml files

<?xml version="1.0" encoding="utf-8"?><PersonF xmlns="" Name="work hard work smart!">  <person Name="Person1">    <ID>1</ID>    <Name>XiaoA</Name>    <Age>59</Age>  </person>  <person Name="Person2" ID="88">    <ID>2</ID>    <Name>XiaoB</Name>    <Age>29</Age>    <Num ID="88" />    <Num>88</Num>  </person>  <person Name="Person3">    <ID Num="999">888</ID>    <Name>XiaoC</Name>    <Age>103</Age>  </person>  <person Name="Person4">    <ID>4</ID>    <Name>XiaoD</Name>    <Age>59</Age>  </person>  <person Name="Person5">    <Name>work hard work smart!</Name>    <ID>32</ID>  </person>  <person Name="Person5">    <Name>work hard work smart!</Name>    <ID>32</ID>  </person></PersonF>

 

The following is a summary of C #'s specific XML operations.

C # xml Operation Summary 2: read, insert, modify, and delete

How to read and write XML documents in. net

C # operating XML selectnodes, selectsinglenode always returns NULL and XPath Introduction

C # Use selectsinglenode to parse XML files with multiple namespaces

 

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.