C#操作XML

來源:互聯網
上載者:User

標籤:des   style   blog   io   color   ar   for   sp   檔案   

 1     XmlDocument doc = new XmlDocument(); 2             //載入xml檔案 3             doc.Load("order.xml"); 4  5             //根節點 6             XmlElement root =  doc.DocumentElement; 7             8             XmlNodeList xnl = root.ChildNodes; 9 10             //11             //讀取標籤的名字12             Console.WriteLine(xnl[0].Name);13 14             Console.WriteLine(xnl[0].InnerText);15             Console.WriteLine(xnl[1].InnerText);16 17             XmlNode items = xnl[2];18 19             //orderitem20             XmlNodeList xnl1 = items.ChildNodes;21 22             foreach (XmlNode node in xnl1)23             {24                 //擷取屬性的值25                 Console.WriteLine(node.Attributes["Name"].Value);26                 Console.WriteLine(node.Attributes["Count"].Value);27             }
1 <?xml version="1.0" encoding="utf-8"?>2 <Order>3   <OrderNo>tj000001</OrderNo>4   <CustomerName>微微</CustomerName>5   <Items>6     <OrderItem Name="媳婦" Count="10" />7     <OrderItem Name="房子" Count="10" />8   </Items>9 </Order>

c#

            XmlDocument doc = new XmlDocument();            //載入xml檔案            doc.Load("books.xml");            //根節點            XmlElement books = doc.DocumentElement;            //books 下的所有book元素            XmlNodeList xnl = books.ChildNodes;            foreach (XmlNode node in xnl)            {                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梅2</Name>    <Price>20</Price>  </Book>  <Book>    <Name>c#圖解教程</Name>    <Price>30</Price>  </Book>  <Book>    <Name>XX梅2</Name>    <Price>20</Price>  </Book></Books>

建立XMl  C#

 1             //記憶體中建立一個xml 2             XmlDocument doc = new XmlDocument(); 3             // 4             XmlDeclaration dec =  doc.CreateXmlDeclaration("1.0", "utf-8", null); 5             doc.AppendChild(dec); 6  7             //根節點 Order 8             XmlElement order = doc.CreateElement("Order"); 9             doc.AppendChild(order);10             //11             XmlElement customerName = doc.CreateElement("CustomerName");12             customerName.InnerText = "微微";13             //14             order.AppendChild(customerName);15             //16             XmlElement orderNo = doc.CreateElement("OrderNo");17             orderNo.InnerText = "tj000001";18             //19             order.AppendChild(orderNo);20             //21             XmlElement items = doc.CreateElement("Items");22             order.AppendChild(items);23 24 25             //26             XmlElement orderItem1 = doc.CreateElement("OrderItem");27             orderItem1.SetAttribute("Name", "媳婦");28             orderItem1.SetAttribute("Count","10");29             items.AppendChild(orderItem1);30 31             //32             XmlElement orderItem2 = doc.CreateElement("OrderItem");33             orderItem2.SetAttribute("Name","房子");34             orderItem2.SetAttribute("Count","10");35             items.AppendChild(orderItem2);36 37             //儲存到檔案38             doc.Save("order.xml");

建立的XML

<?xml version="1.0" encoding="utf-8"?><Order>  <CustomerName>微微</CustomerName>  <OrderNo>tj000001</OrderNo>  <Items>    <OrderItem Name="媳婦" Count="10" />    <OrderItem Name="房子" Count="10" />  </Items></Order>

 

C#操作XML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.