<1>
Creating an XML document
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.xml;namespace Create xml{class program {static void Main (string[] args) {XMLDOCU ment doc = new XmlDocument (); Create an XML document//Create an XML document description. That is: Create <?xml version= "1.0" encoding= "Utf-8"?> this section xmldeclaration Dec = doc. Createxmldeclaration ("1.0", "Utf-8", null); Add an XML document description to the XML document in Doc. AppendChild (DEC); Create the root node of an XML document------I named the root node of the XML document <Books> XmlElement Books = doc. CreateElement ("Books"); Add the root node you just created to this XML document in Doc. AppendChild (books); --------------------------------to books This root node to add a book child node, named Boo1//to the root node of the XML document books Create child nodes XmlElement Bo Ok1 = doc. CreateElement ("book"); Add the Book1 to the root node books. AppendChild (BOOK1); Add a child node of name to Book1 XmlElement name1 = doc. CreateElement ("Name"); Assigns a value name1 to the NAME1 child node. InnerText = "Journey to the Monkey"; Add name1 to Boo1 in Book1. AppendChild (NAME1); Add a child node of price to Book1 XmlElement Price1 = doc. CreateElement ("Price"); Assigns a value Price1 to the Price1 child node. InnerText = "50"; Add this child node to the Price1 to Book1 Book1. AppendChild (Price1); Add a des child node to book1 XmlElement des1 = doc. CreateElement ("Des"); Assigns a value des1 to the DES1 child node. InnerText = "Myth story"; Add this child node to the des1 to Book1 Book1. AppendChild (DES1); --------------------------------to books This root node to add another book child node, named Boo2//to the root node of the XML document books Create child nodes XmlElement Book2 = doc. CreateElement ("book"); Add the Book1 to the root node books. AppendChild (BOOK2); Add a child node of name to Book1 XmlElement name2 = doc. CreateElement ("Name"); Assigns a value name2 to the NAME1 child node. InnerText = "Red mansions"; Add name1 to Boo1 boOk2. AppendChild (name2); Add a child node of price to Book1 XmlElement Price2 = doc. CreateElement ("Price"); Assigns a value price2 to the Price1 child node. InnerText = "60"; Add this child node to the Price1 to Book1 Book2. AppendChild (PRICE2); Add a des child node to book1 XmlElement Des2 = doc. CreateElement ("Des"); Assigns a value des2 to the DES1 child node. InnerText = "Historical theme"; Add this child node to the des1 to Book1 Book2. AppendChild (Des2); Doc. Save ("books.xml"); Console.WriteLine ("Saved successfully"); Console.readkey (); } }}
The above code creates the specific contents of the XML document
To create an XML document with attributes
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.xml;namespace Create xml{class program {static void Main (string[] args) with attributes {XML Document doc = new XmlDocument ();//Create an XML document//Create an XML document description. That is: Create <?xml version= "1.0" encoding= "Utf-8"?> this section xmldeclaration Dec = doc. Createxmldeclaration ("1.0", "Utf-8", null); Add an XML document description to the XML document in Doc. AppendChild (DEC); Create the root node of the XML document------I set the root node of the XML document to <Order> XmlElement order = doc. CreateElement ("Order"); Add the root node you just created to this XML document in Doc. AppendChild (order); --------------------------------Add a CustomerName child node to the order root node, named CustomerName//To create a child node for the root node order of the XML document Customern Ame XmlElement customerName = doc. CreateElement ("CustomerName"); Assign value to customerName this child node Customername.innertext = "Jacky"; Will customername this child nodeAdd to the root node under order. AppendChild (CustomerName); Create a child node for the root node order of the XML document ordernumber XmlElement OrderNumber = doc. CreateElement ("OrderNumber"); Assign a value Ordernumber.innertext = "dd00001" to the OrderNumber child node; Add OrderNumber This child node to the root node under order. AppendChild (OrderNumber); Create a child node for the root node order of the XML document items XmlElement items = doc. CreateElement ("Items"); Add the items sub-node to the root node under order. AppendChild (items); Create a orderItem1 node XmlElement orderItem1 = Doc. CreateElement ("OrderItem"); Add attribute Orderitem1.setattribute ("Name", "phone") to orderItem1 this node; Orderitem1.setattribute ("Count", "1"); Add the OrderItem1 node to the items node under items. AppendChild (ORDERITEM1); Then create a orderItem2 node XmlElement orderItem2 = Doc. CreateElement ("OrderItem"); Add the attribute Orderitem2.setattribute ("Name", "Computer") to the ORDERITEM2 node; Orderitem2.setattribute ("Count", "2"); Add the ORDERITEM2 node to the items node under items. AppendChild (ORDERITEM2); Doc. Save ("Order.xml"); Console.WriteLine ("Saved successfully"); Console.readkey (); } }}
The above code creates the specific content of the XML code with the attribute
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # code reads and writes XML