Detailed description of simple XML reading and writing

Source: Internet
Author: User
It is known that there is an xml file (bookstore. xml) as follows: & lt ;? Xmlversion & quot; 1.0 & quot; encoding & quot; gb2312 & quot ;? & Gt; & lt; bookstore & gt; it is known that there is an xml file (bookstore. xml) as follows:

 
 
  
   Oberon's LegacyCorets, Eva
   
    5.95
   
  
 

1. forward Insert Node:

XmlDocument xmlDoc = new XmlDocument (); xmlDoc. Load ("bookstore. xml"); XmlNode root = xmlDoc. SelectSingleNode ("bookstore"); // search
 
  
XmlElement xe1 = xmlDoc. CreateElement ("book"); // create
  
   
Node xe1.SetAttribute ("genre", ""); // Set the node genre attribute xe1.SetAttribute ("ISBN", "2-3631-4 "); // Set the ISBN attribute of the node XmlElement xesub1 = xmlDoc. createElement ("title"); xesub1.InnerText = "CS from getting started to proficient"; // set the text node xe1.AppendChild (xesub1); // add
   
    
XmlElement xesub2 = xmlDoc in the node. createElement ("author"); xesub2.InnerText = ""; xe1.AppendChild (xesub2); XmlElement xesub3 = xmlDoc. createElement ("price"); xesub3.InnerText = "58.3"; xe1.AppendChild (xesub3); root. appendChild (xe1); // add
    
     
XmlDoc. Save ("bookstore. xml ");
    
   
  
 

// ================================================ ==========
Result:

 
 
  
   Oberon's LegacyCorets, Eva
   
    
5.95
   
  
  
   CS from entry to entryHou Jie
   
    
58.3
   
  
 

2. modify the node: change the genre value of the node whose genre attribute value is "Li zanxiang" to "update Li zanxiang", and change the text of the child node of the node to "Yasheng ".

To "Yasheng ". XmlNodeList nodeList = xmlDoc. selectSingleNode ("bookstore "). childNodes; // Obtain all subnodes of the bookstore node foreach (XmlNode xn in nodeList) // traverse all subnodes {XmlElement xe = (XmlElement) xn; // Convert the subnode type to the XmlElement type if (xe. getAttribute ("genre") = "") // if the genre attribute value is "" {xe. setAttribute ("genre", "update Lizan red"); // modify this attribute to "update Lizan red" XmlNodeList nls = xe. childNodes; // continue to obtain the foreach (XmlNode xn1 in nls) of all the child nodes of xe // traverse {XmlElement xe2 = (XmlElement) xn1; // if (xe2.Name = "author") // modify the break if {xe2.InnerText = "Yasheng" is found; // Find and exit.} break ;}}

XmlDoc. Save ("bookstore. xml"); // Save.
// ================================================ ================

The final result is:

 
 
  
   Oberon's LegacyCorets, Eva
   
    
5.95
   
  
  
   CS from entry to entryYasheng
   
    
58.3
   
  
 

3. delete Node genre attribute, delete 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:

 
 
  
   Oberon's LegacyCorets, Eva
   
    5.95
   
  
  
 

4. display all data.

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

The above is a detailed description of the simple reading and writing of XML. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.