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>
Reads an attribute from a node.
XmlDocument doc = new XmlDocument ();
Doc. Load ("config. xml"); // you can add another path, for example, D: \ config. xml.
XmlNode xnuser = doc. SelectSingleNode ("userdata ");
String flag = xnuser. Attributes ["createuser"]. InnerText;
Read the value from the node
XmlDocument doc = new XmlDocument ();
Doc. Load ("config. xml ");
XmlNode xnserver = doc. SelectSingleNode ("userdata/dataconnection/server ");
Modify node attributes
XmlDocument doc = new XmlDocument ();
Doc. Load ("config. xml ");
XmlNode xnuser = doc. SelectSingleNode ("userdata ");
Xnuser. Attributes ["createuser"]. InnerText = "false ";
Doc. Save ("config. xml ");
Append node
XmlDocument doc = new XmlDocument ();
XmlTextReader reader = new XmlTextReader ("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 end of the xml text
Root. AppendChild (tagMessage );
Reader. Close (); // Close XmlTextReader
Doc. Save ("config. xml"); // Save the xml file