Study notes, please correct me if you have any mistakes. The number is also please point out, thank you.
There is an XML file named "Userinfo.xml", which operates as follows:
<?xml version= 1.0 " Encoding=" utf-8 " Standalone=" no "?><users> <user1 name =" Zhang San /span> Password =" 123 > I am Zhang San </user1> <user2 name =" John Doe Password =" > I am John Doe </user2></users>
1 Insert a node, add properties, save (note the path where the XML file is stored)
XmlDocument xmldoc =NewXmlDocument ();//declaring an XML fileXml. Load ("C:/userinfo.xml");//read the XML according to the pathXmlNodeList childlist = Xmldoc.selectsinglenode ("Users"). ChildNodes;//Find-Get all child nodes under ObjectsXmlNodeList nodelist = xmldoc.createelement ("User3");//Create a nodeXel. SetAttribute ("name","Wang er");//Set the node Name propertyXel. SetAttribute ("Password","123");//Set the node password propertyXel. innertext="I'm Wang er ."; root. AppendChild (xel);//Add to NodeXmldoc.save ("C:/userinfo.xml");
Run Complete effect:
<?xml version="1.0"encoding="UTF-8"Standalone="No"?><users> <user1 name ="Zhang San"Password ="123"> I am Zhang San </user1> <user2 name ="John Doe"Password ="123"> I am John Doe </user2> <user3 name ="Wang er"Password ="123"> I am Wang er </user3></users>
2 modifying nodes
Change the User3 password to "1" and modify the content to "I am Wangxiao"
XmlDocument xmldoc =NewXmlDocument ();//declaring an XML fileXml. Load ("C:/userinfo.xml");//read the XML according to the pathXmlNodeList root = Xmldoc.selectsinglenode ("Users"). ChildNodes;//Find-Get all child nodes under ObjectsXmlElement User3 = (XmlElement) root. selectSingleNode ("User3");//Create a nodeUser3. SetAttribute (the password","1");//Set the node Name propertyUser3. innertext="I'm Wangxiao .";Xmldoc.save ("C:/userinfo.xml");
Run Complete effect:
<?xml version="1.0"encoding="UTF-8"Standalone="No"?><users> <user1 name ="Zhang San"Password ="123"> I am Zhang San </user1> <user2 name ="John Doe"Password ="123"> I am John Doe </user2> <user3 name ="Wang er"Password ="1"> I am Wangxiao </user3></users>
3 Deleting Attributes and nodes
XmlDocument xmldoc =NewXmlDocument ();//declaring an XML fileXml. Load ("C:/userinfo.xml");//read the XML according to the pathXmlNodeList root = Xmldoc.selectsinglenode ("Users"). ChildNodes;//Find-Get all child nodes under ObjectsXmlElement User3 = (XmlElement) root. selectSingleNode ("User3");//Get a collection of sibling nodes of the same nameUser3. RemoveAttribute ("Password");//Delete the specified propertyRoot. RemoveChild (Root. selectSingleNode ("User2"));//removechild to remove a node from the list of child nodesUser3. innertext="I'm Wangxiao ."; Xmldoc.save ("C:/userinfo.xml");
The effect is as follows:
<?xml version= 1.0 " Encoding=" utf-8 " Standalone=" no "?><users> <user1 name =" Zhang San /span> Password =" 123 > I am Zhang San </user1> <user3 name =" Wang er Password =" 1 > I am Wangxiao </user3></users>
4 Traversing a node
New XmlDocument (); // declares an XML file XML. Load ("c:/userinfo.xml"); // read the XML according to the path XmlNodeList root = Xmldoc.selectsinglenode ("users"// Lookup- Get all the child nodes under Objects foreach in root. ChildNodes)// traverse node Debug.Log (xn. InnerText);
01 Operations xml-c# operation XML File Basics