C#讀寫xml文件
[1] C#讀寫xml文件
[2] C#讀寫xml文件
[2] C#讀寫xml文件
c#讀寫xml文件
已知有一個XML文件(bookstore.xml)如下:
Code <?xml version="1.0" encoding="gb2312"?> <bookstore> <book genre="fantasy" ISBN="2-3631-4"> <title>Oberon's Legacy</title> <author>Corets, Eva</author> <price>5.95</price> </book> </bookstore>
Code XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load("bookstore.xml"); XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore> XmlElement xe1=xmlDoc.CreateElement("book");//創建一個<book>節點 xe1.SetAttribute("genre","李贊紅");//設置該節點genre屬性 xe1.SetAttribute("ISBN","2-3631-4");//設置該節點ISBN屬性 XmlElement xesub1=xmlDoc.CreateElement("title"); xesub1.InnerText="CS從入門到精通";//設置文本節點 xe1.AppendChild(xesub1);//添加到<book>節點中 XmlElement xesub2=xmlDoc.CreateElement("author"); xesub2.InnerText="候捷"; xe1.AppendChild(xesub2); XmlElement xesub3=xmlDoc.CreateElement("price"); xesub3.InnerText="58.3"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<bookstore>節點中 xmlDoc.Save("bookstore.xml");
Code <?xml version="1.0" encoding="gb2312"?> <bookstore> <book genre="fantasy" ISBN="2-3631-4"> <title>Oberon's Legacy</title> <author>Corets, Eva</author> <price>5.95</price> </book> <book genre="李贊紅" ISBN="2-3631-4"> <title>CS從入門到精通</title> <author>候捷</author> <price>58.3</price> </book> </bookstore>
Code
Code <?xml version="1.0" encoding="gb2312"?> <bookstore> <book genre="fantasy" ISBN="2-3631-4"> <title>Oberon's Legacy</title> <author>Corets, Eva</author> <price>5.95</price> </book> <book genre="update李贊紅" ISBN="2-3631-4"> <title>CS從入門到精通</title> <author>亞勝</author> <price>58.3</price> </book> </bookstore>
[第1頁][第2頁]
全站熱搜