C # To operate XML files (read/write, modify, delete, and delete all contacts)

Source: Internet
Author: User

Common Operations:

XmlDocument xmlDoc = new XmlDocument (); <br/> xmlDoc. Load ("bookstore. xml"); <br/> // xmlDoc. LoadXml ("<! -- L version =/"1.0/" encoding =/"gb2312/--> <BOOKSTORE> </BOOKSTORE>"); <br/> XmlNode root = xmlDoc. selectSingleNode ("bookstore"); // find <BOOKSTORE> </BOOKSTORE> <br/> XmlElement xe1 = xmlDoc. createElement ("book"); // create a <BOOK> </BOOK> node <br/> xe1.SetAttribute ("genre", "Li zanhong "); // set genre attributes of the node <br/> xe1.SetAttribute ("ISBN", "2-3631-4 "); // set the ISBN attribute of the node </p> <p> XmlElement xesub1 = xmlDoc. createElement ("title"); <br/> xesub1.InnerText = "CS from entry to entry"; // set a text node <br/> xe1.AppendChild (xesub1 ); // Add it to the <BOOK> </BOOK> node <br/> XmlElement xesub2 = xmlDoc. createElement ("author"); <br/> xesub2.InnerText = ""; <br/> xe1.AppendChild (xesub2); </p> <p> XmlElement xesub3 = xmlDoc. createElement ("price"); <br/> xesub3.InnerText = "58.3"; <br/> xe1.AppendChild (xesub3); </p> <p> root. appendChild (xe1); // Add it to the <BOOKSTORE> </BOOKSTORE> node <br/> xmlDoc. save ("bookstore. xml "); <br/>
     

It is known that there is an XML file (bookstore. xml) as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon's Legacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
</Bookstore>

1. Insert a <book> node to the <bookstore> node:
 

XmlDocument xmlDoc = new XmlDocument (); <br/> xmlDoc. Load ("bookstore. xml"); <br/> // xmlDoc. LoadXml ("<! -- L version =/"1.0/" encoding =/"gb2312/--> <BOOKSTORE> </BOOKSTORE>"); <br/> XmlNode root = xmlDoc. selectSingleNode ("bookstore"); // find <BOOKSTORE> </BOOKSTORE> <br/> XmlElement xe1 = xmlDoc. createElement ("book"); // create a <BOOK> </BOOK> node <br/> xe1.SetAttribute ("genre", "Li zanhong "); // set genre attributes of the node <br/> xe1.SetAttribute ("ISBN", "2-3631-4 "); // set the ISBN attribute of the node </p> <p> XmlElement xesub1 = xmlDoc. createElement ("title"); <br/> xesub1.InnerText = "CS from entry to entry"; // set a text node <br/> xe1.AppendChild (xesub1 ); // Add it to the <BOOK> </BOOK> node <br/> XmlElement xesub2 = xmlDoc. createElement ("author"); <br/> xesub2.InnerText = ""; <br/> xe1.AppendChild (xesub2); <br/> XmlElement xesub3 = xmlDoc. createElement ("price"); <br/> xesub3.InnerText = "58.3"; <br/> xe1.AppendChild (xesub3); </p> <p> root. appendChild (xe1); // Add it to the <BOOKSTORE> </BOOKSTORE> node <br/> xmlDoc. save ("bookstore. xml "); <br/>
   

// ================================================ ==========
Result:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon's Legacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Book>
</Bookstore>

2. Modify the node: Change the genre value of the node whose genre attribute value is "Li zenhong" to "update Li zenhong ", modify the text of the child node <author> of the node to "Yasheng ".
 

  1. XmlNodeList nodeList = xmlDoc. selectSingleNode ("bookstore "). childNodes; // obtain all the subnodes of the bookstore node <br/> foreach (XmlNode xn in nodeList) // traverse all sub-nodes <br/>{< br/> XmlElement xe = (XmlElement) xn; // convert the subnode type to the XmlElement type <br/> if (xe. getAttribute ("genre") = "") // If the genre attribute value is "" <br/>{< br/> xe. setAttribute ("genre", "update lizan red"); // modify the attribute to "update lizan red" </p> <p> XmlNodeList nls = xe. childNodes; // continue to obtain all the child nodes of the xe subnode <Br/> foreach (XmlNode xn1 in nls) // traverse <br/>{< br/> XmlElement xe2 = (XmlElement) xn1; // Conversion Type <br/> if (xe2.Name = "author") // if <br/>{< br/> xe2.InnerText = "Yasheng" is found "; // modify <br/> break; // find and exit <br/>}< br/> break; <br/>}</p> <p> xmlDoc. save ("bookstore. xml "); // save.
    

// ================================================ ================
The final result is:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon's Legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "Update Li zanhong" ISBN = "2-3631-4">
<Title> Cs from entry to entry </title>
<Author> Yasheng </author>
<Price> 58.3 </price>
</Book>
</Bookstore>

3. Delete the genre attribute of the <book genre = "Fantasy" ISBN = "2-3631-4"> node, delete the <book genre = "Update lizan red" ISBN = "2-3631-4"> node.

Xmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore "). childnodes; foreach (xmlnode Xn in xnl) {xmlelement Xe = (xmlelement) xn; If (Xe. getattribute ("genre") = "Fantasy") {Xe. removeattribute ("genre"); // Delete genre attribute} else if (Xe. getattribute ("genre") = "Update lizan red") {Xe. removeall (); // delete all content of the node} xmldoc. save ("Bookstore. XML ");

// ================================================ =====
The final result is:
<? XML version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book ISBN = "2-3631-4">
<Title> Oberon's legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book>
</Book>
</Bookstore>

4. display all data.
 

  1. XmlNode xn = xmlDoc. selectSingleNode ("bookstore"); </p> <p> XmlNodeList xnl = xn. childNodes; </p> <p> foreach (XmlNode xnf in xnl) <br/> {<br/> XmlElement xe = (XmlElement) xnf; <br/> Console. writeLine (xe. getAttribute ("genre"); // display the property value <br/> Console. writeLine (xe. getAttribute ("ISBN"); </p> <p> XmlNodeList xnf1 = xe. childNodes; <br/> foreach (XmlNode xn2 in xnf1) <br/>{< br/> Console. writeLine (xn2.InnerText); // displays the child node text <br/>}< br/>}

 

Reference this page address: http://www.yongfa365.com/item/CZhongCaoZuoXMLWenJianDuXieGaiShanQuanJieChuGenQiTaYuYanCaoZuoXMLChaBuDuoYiYang.html

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.