XmlDocument example code for operating xml documents

Source: Internet
Author: User
This article describes the sample code for XmlDocument operations. it has some reference value. let's take a look at it. XmlDocument example code for operating xml documents

  
   
     
   
    xml test
      
   
    sa
      
   
    sa
     
    
     
   
    xml document
     
  
 

Reads an attribute from a node.

   XmlDocument doc=newXmlDocument();   doc.Load("config.xml");   XmlNodexnuser=doc.SelectSingleNode("userdata");   stringflag=xnuser.Attributes["createuser"].InnerText;

Read the value from the node

   XmlDocument doc=newXmlDocument();   doc.Load("config.xml");   XmlNode xnserver =doc.SelectSingleNode("userdata/dataconnection/server");

Modify node attributes

   XmlDocument doc=newXmlDocument();   doc.Load("config.xml");   XmlNodexnuser=doc.SelectSingleNode("userdata");  xnuser.Attributes["createuser"].InnerText="false";   doc.Save("config.xml");

Append node

XmlDocument doc = newXmlDocument (); XmlTextReader reader = newXmlTextReader ("config. xml "); doc. load ("config. xml "); XmlElement root = doc. documentElement; // get the root node XmlElement tagMessage = doc. createElement ("net"); XmlElement tagText = doc. createElement ("name"); tagText. innerText = netname; tagMessage. appendChild (tagText); // append to the root of the xml text. appendChild (tagMessage); reader. close (); // Close XmlTextReader doc. save ("config. xml "); // Save the xml file

The above is the details of the sample code used to operate xml documents in XmlDocument. For more information, see other related articles in the first PHP community!

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.