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