XML addition, deletion, and modification

Source: Internet
Author: User
1. after removing the two breaks in the node code, the code can traverse and modify the entire xml document. Therefore, foreach can traverse and modify XML files, however, you cannot add or delete traversal. Sample XML file (bookstore. xml)

 
   
      
   Oberon's Legacy    Corets, Eva    
   
    5.95
     
  
 

1. forward Insert Node:

XmlDocument xmlDoc = new XmlDocument (); xmlDoc. Load ("E: \ 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 ("E: \ bookstore. xml ");
     
    
   
  


The modified xml file is:


  
    
       
    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 ".

XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load ("E: \ bookstore. xml "); 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; // conversion type // title, author, and price are all obtained in xe2.Name if (xe2.Name = "author") // if {xe2.InnerText = "Yasheng" is found "; // modify the break. // Find and exit.} break;} xmlDoc. save ("bookstore. xml "); // save.


Note:

1. after removing the two breaks in the node code, the code can traverse and modify the entire xml document. Therefore, foreach can traverse and modify XML files, however, you cannot add or delete traversal.

2. Note the conversion of for and foreach in xml file operations: for XML traversal, add nodes and modify attribute small examples
The modified xml file is:

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


3. delete Node genre attribute, delete Node.

XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load ("E: \ bookstore. xml "); 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") = "") {// RemoveAll removes all information under the specified node, but retains the node xe. removeAll (); // delete all content of the node} x MlDoc. save ("E: \ bookstore. xml "); note: foreach is used when operating the XML file. Therefore, after deleting all the content of a node, the foreach loop will jump out. The modified xml file is:
 
 
      
   Oberon's LegacyCorets, Eva    
   
    
5.95
     
    
    
  
 4. display all data. XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load ("E: \ bookstore. xml "); XmlNode xn = xmlDoc. selectSingleNode ("bookstore"); XmlNodeList xnl = xn. childNodes; foreach (XmlNode xnf in xnl) {XmlElement xe = (XmlElement) xnf; MessageBox. show (xe. getAttribute ("genre"); // display the MessageBox attribute value. show (xe. getAttribute ("ISBN"); XmlNodeList xnf1 = xe. childNodes; foreach (XmlNode xn2 in xnf1) {MessageBox. show (xn2.InnerText); // displays the child node text} original address =

The above content is added, deleted, and modified in XML. For more information, see PHP Chinese website (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.