XML addition, deletion, modification, and query examples

Source: Internet
Author: User
XML addition, deletion, modification, and query example 1. an XML file (bookstore. xml) is known as follows:

 
   
      
   Oberon's Legacy    Corets, 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 ".

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 get all the sub-nodes of xe sub-nodes foreach (XmlNode xn1 in nls) // traverse {XmlElement xe2 = (Xm LElement) xn1; // The conversion type 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 Legacy    Corets, 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 }}

2 front-end code: html

<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.OpXMLFile.Main" %>            Main        
         
         
         
                     

XML file dbGuest. xml

 
   
      
   
    
Aaa
       
   
    
Shanghai
       
   
    
Aaa@263.net
       
   
    
OK
       
   
    
2004-07-12T00: 00: 00.0000000 + 08: 00
     
    
      
   
    
Shaoazhd
       
   
    
Beijing
       
   
    
Sss@22.net
       
   
    
Afsa
       
   
    
2004-7-12 15:07:39
     
    
      
   
    
Guset
       
   
    
Shanghai
       
   
    
Sfaf@22.net
     
    
      
   
    
Guset
       
   
    
Shanghai
       
   
    
Ss@22.net
     
  
 Using System; using System. collections; using System. componentModel; using System. data; using System. drawing; using System. web; using System. web. sessionState; using System. web. UI; using System. web. UI. webControls; using System. web. UI. htmlControls; using System. xml; using System. xml. XPath; namespace DsAndXML. opXMLFile {////// Summary of Main. ///Public class Main: System. web. UI. page {protected System. web. UI. webControls. button btnQuery; protected System. web. UI. webControls. button btnChange; protected System. web. UI. webControls. button btnDelete; protected System. web. UI. webControls. button btnAdd; protected System. web. UI. webControls. dropDownList ddlName; protected System. web. UI. webControls. label Label1; protected System. web. UI. webControls. labe L Label2; protected System. web. UI. webControls. label lbEmail; protected System. web. UI. webControls. textBox tbNewMail; protected System. web. UI. webControls. label Label3; protected System. web. UI. webControls. dataGrid dgShow; private void Page_Load (object sender, System. eventArgs e) {// place the user code here to initialize the page if (! IsPostBack) Bind ();} private void Bind () {DataSet ds = new DataSet (); ds. readXml (Server. mapPath (". \ db \ dbGuest. xml "); dgShow. dataSource = ds. tables [0]. defaultView; dgShow. dataBind (); XmlDocument doc = new XmlDocument (); doc. load (Server. mapPath (". \ db \ dbGuest. xml "); XmlNodeList elemList = doc. getElementsByTagName ("Name"); ddlName. items. clear (); for (int I = 0; I
 
  
0) {if (node [0]. innerText = ddlName. selectedItem. text) {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 if (xe2.Name = "Email") // if {xe2.InnerText = tbNewMail is found. text; // modify the break; // locate and exit.} break ;}} xmlDoc. save (Server. mapPath (". \ db \ dbGuest. xml "); Bind ();} private void btnDelete_Click (object sender, System. eventArgs e) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (Server. mapPath (". \ db \ dbGuest. xml "); XmlNodeList xnl = xmlDoc. selectSingleNode ("dbGuest "). childNodes; foreach (XmlNode xn in xnl) {XmlElement xe = (XmlElement) xn; XmlNodeList node = xe. getElementsByTagName ("Name"); if (node. count> 0) {if (node [0]. innerText = ddlName. selectedItem. text) xe. removeAll (); // delete all the content of the node break;} xmlDoc. save (Server. mapPath (". \ db \ dbGuest. xml "); Bind ();} private void btnAdd_Click (object sender, System. eventArgs e) {XmlDocument xmlDoc = new XmlDocument (); xmlDoc. load (Server. mapPath (". \ db \ dbGuest. xml "); XmlNode root = xmlDoc. selectSingleNode ("dbGuest"); // find
  
   
XmlElement xe1 = xmlDoc. CreateElement ("User"); // create
   
    
Node XmlElement xesub1 = xmlDoc. CreateElement ("Name"); xesub1.InnerText = "Guset"; // set the text node xe1.AppendChild (xesub1); // add it
    
     
XmlElement xesub2 = xmlDoc in the node. createElement ("City"); xesub2.InnerText = "Shanghai"; xe1.AppendChild (xesub2); XmlElement xesub3 = xmlDoc. createElement ("Email"); xesub3.InnerText = "ss@22.net"; xe1.AppendChild (xesub3); root. appendChild (xe1); // add
     
      
XmlDoc. Save (Server. MapPath (". \ db \ dbGuest. xml"); Bind () ;}} in the node ();}}}
     
    
   
  
 

The above is the content of the example for adding, deleting, modifying, and querying 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.