向XML文檔加入屬性
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(@"C:/Inetpub/wwwroot/CH12/phone.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//創建一個<book>節點
xe1.SetAttribute("Class","政治");//設置該節點Class屬性
xe1.SetAttribute("ISBN","3-10-5");//設置該節點ISBN屬性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="毛澤東思想";//設置文本節點
xe1.AppendChild(xesub1);//添加到<book>節點中
root.AppendChild(xe1);//添加到<bookstore>節點中
xmlDoc.Save(@"C:/Inetpub/wwwroot/CH12/phone.xml");
}
{
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(@"C:/Inetpub/wwwroot/CH12/phone.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//創建一個<book>節點
xe1.SetAttribute("Class","政治");//設置該節點Class屬性
xe1.SetAttribute("ISBN","3-10-5");//設置該節點ISBN屬性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="毛澤東思想";//設置文本節點
xe1.AppendChild(xesub1);//添加到<book>節點中
root.AppendChild(xe1);//添加到<bookstore>節點中
xmlDoc.Save(@"C:/Inetpub/wwwroot/CH12/phone.xml");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.