C # manipulating XML

Source: Internet
Author: User

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>&lt;B&gt;123&lt;/b&gt;</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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.