C # Simple manipulation of XML

Source: Internet
Author: User
The XML file format is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>  <userdata createuser= "false" >  <dataconnection>  <server>localhost</server>  <uid>sa</uid>  <pwd></pwd>  < /dataconnection>  <net>  <name>jiayuan</name>  </net>  </userdata >

Read a property in a node

XmlDocument doc=new XmlDocument ();  Doc. Load ("config +");//You can add the path again: such as D:\config.xml  XmlNode Xnuser=doc. selectSingleNode ("UserData");  String Flag=xnuser. attributes["CreateUser"]. InnerText;

Reading values from a node

XmlDocument doc=new XmlDocument ();  Doc. Load ("config +");  XmlNode xnserver = doc. selectSingleNode ("Userdata/dataconnection/server");

Modify the properties of a node

XmlDocument doc=new XmlDocument ();  Doc. Load ("config +");  XmlNode Xnuser=doc. selectSingleNode ("UserData");  Xnuser. attributes["CreateUser"]. Innertext= "false";  Doc. Save ("config +");

Append node

XmlDocument doc = new XmlDocument ();  XmlTextReader reader = new XmlTextReader ("config +");  Doc. Load ("config +");  XmlElement root = Doc. DocumentElement; Gets the root node  XmlElement tagmessage = doc. createelement ("net");  XmlElement Tagtext = doc. createelement ("name");  Tagtext.innertext = NetName;  Tagmessage.appendchild (Tagtext); Append to the last root of the XML text  . AppendChild (tagmessage);  Reader. Close (); Close XmlTextReader  Doc. Save ("config +"); Save the XML file
Related Article

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.