已知有一個XML文件(bookstore.XML)如下:
<?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>
1、往<bookstore>節點中插入一個<book>節點:
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=&qu