xml操作小結

using System.Xml;

//初始化一個xml實例
XmlDocument xml=new XmlDocument();

//導入指定xml文件
xml.Load(path);
xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));

//指定一個節點
XmlNode root=xml.SelectSingleNode("/root");

//獲取節點下所有直接子節點
XmlNodeList childlist=root.ChildNodes;

//判斷該節點下是否有子節點
root.HasChildNodes;

//獲取同名同級節點集合
XmlNodeList nodelist=xml.SelectNodes("/Root/News");

//生成一個新節點
XmlElement node=xml.CreateElement("News");

//將節點加到指定節點下,作爲其子節點
root.AppendChild(node);

//將節點加到指定節點下某個子節點前
root.InsertBefore(node,root.ChildeNodes[i]);

//爲指定節點的新建屬性並賦值
node.SetAttribute("id","11111");

//爲指定節點添加子節點
root.AppendChild(node);

//獲取指定節點的指定屬性值
string id=node.Attributes["id"].Value;

//獲取指定節點中的文本
string content=node.InnerText;

//保存XML文件
string path=Server.MapPath("~/file/bookstore.xml");
xml.Save(path);
//or use :xml.Save(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));

發佈了23 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章