Declare, create, create a root node, add a child node of Books, and finally save the document (if the file already exists, update it; if not, create the file ), none of you can understand the definition, creation, and creation of the root node, and addition of the Books sub-node to save the document (if the file already exists, update it; if not, this file is created ).
The code is as follows:
Protected void button#click (object sender, EventArgs e) // create xml {// declare XmlDocument x = new XmlDocument (); // create XmlDeclaration xd = x. createXmlDeclaration ("1.0", "GB2312", null); x. appendChild (xd); // create the root node XmlElement = x. createElement ("Books"); x. appendChild (element); // add the child node XmlNode book of Books = x. createElement ("Book"); // add the XmlElement bookname = x. createElement ("name"); bookname. innerText = "three kingdoms"; // add it to the book on the node step by step. appendChild (bookname); element. appendChild (book); x. appendChild (element); // save the document (if the file already exists, update it; if not, create the file) x. save (@ "F:/Books. xml ");}
The above is the sample code sharing for creating root nodes and subnodes in xml. For more information, see other related articles in the first PHP community!