1XmlDocument doc =NewXmlDocument ();2 //loading an XML file3Doc. Load ("Order.xml");4 5 //root node6XmlElement root =Doc. documentelement;7 8XmlNodeList XNL =Root. ChildNodes;9 Ten // One //read the name of the label AConsole.WriteLine (xnl[0]. Name); - -Console.WriteLine (xnl[0]. InnerText); theConsole.WriteLine (xnl[1]. InnerText); - -XmlNode items = xnl[2]; - + //OrderItem -XmlNodeList XNL1 =items. ChildNodes; + A foreach(XmlNode nodeinchxnl1) at { - //gets the value of the property -Console.WriteLine (node. attributes["Name"]. Value); -Console.WriteLine (node. attributes["Count"]. Value); -}
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Order>3 <OrderNo>tj000001</OrderNo>4 <CustomerName>Slightly</CustomerName>5 <Items>6 <OrderItemName= "Daughter -in-law"Count= "Ten" />7 <OrderItemName= "House"Count= "Ten" />8 </Items>9 </Order>
C#
XmlDocument doc =NewXmlDocument (); //loading an XML fileDoc. Load ("books.xml"); //root nodeXmlElement books =Doc. DocumentElement; //all book elements under the booksXmlNodeList XNL =Books. ChildNodes; foreach(XmlNode nodeinchxnl) {Console.WriteLine (node. childnodes[0]. InnerText); Console.WriteLine (node. childnodes[1]. InnerText); }
Xml
<?XML version= "1.0" encoding= "Utf-8"?><Books> < Book> <Name><B>123</b></Name> < Price>10</ Price> </ Book> < Book> <Name>12 Plum 2</Name> < Price>20</ Price> </ Book> < Book> <Name>C # Illustrated Tutorials</Name> < Price>30</ Price> </ Book> < Book> <Name>xxPlum 2</Name> < Price>20</ Price> </ Book></Books>
Creating XML C #
1 //create an XML in memory2XmlDocument doc =NewXmlDocument ();3 //4XmlDeclaration Dec = doc. Createxmldeclaration ("1.0","Utf-8",NULL);5 Doc. AppendChild (DEC);6 7 //root node Order8XmlElement order = doc. CreateElement ("Order");9 Doc. AppendChild (order);Ten // OneXmlElement customerName = doc. CreateElement ("CustomerName"); ACustomername.innertext ="slightly"; - // - order. AppendChild (customerName); the // -XmlElement OrderNo = doc. CreateElement ("OrderNo"); -Orderno.innertext ="tj000001"; - // + order. AppendChild (OrderNo); - // +XmlElement items = doc. CreateElement ("Items"); A order. AppendChild (items); at - - // -XmlElement orderItem1 = doc. CreateElement ("OrderItem"); -Orderitem1.setattribute ("Name","wife"); -Orderitem1.setattribute ("Count","Ten"); in items. AppendChild (ORDERITEM1); - to // +XmlElement orderItem2 = doc. CreateElement ("OrderItem"); -Orderitem2.setattribute ("Name","House"); theOrderitem2.setattribute ("Count","Ten"); * items. AppendChild (ORDERITEM2); $ Panax Notoginseng //Save to file -Doc. Save ("Order.xml");
The XML created
<?XML version= "1.0" encoding= "Utf-8"?><Order> <CustomerName>Slightly</CustomerName> <OrderNo>tj000001</OrderNo> <Items> <OrderItemName= "Daughter -in-law"Count= "Ten" /> <OrderItemName= "House"Count= "Ten" /> </Items></Order>
C # manipulating XML