這篇文章給大家分享的是有關(guān).net怎么操作xml的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
代碼如下:
using System.Xml;
//初始化一個(gè)xml實(shí)例
XmlDocument xml=new XmlDocument();
//導(dǎo)入指定xml文件
xml.Load(path);
xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));
//指定一個(gè)節(jié)點(diǎn)
XmlNode root=xml.SelectSingleNode("/root");
//獲取節(jié)點(diǎn)下所有直接子節(jié)點(diǎn)
XmlNodeList childlist=root.ChildNodes;
//判斷該節(jié)點(diǎn)下是否有子節(jié)點(diǎn)
root.HasChildNodes;
//獲取同名同級(jí)節(jié)點(diǎn)集合
XmlNodeList nodelist=xml.SelectNodes("/Root/News");
//生成一個(gè)新節(jié)點(diǎn)
XmlElement node=xml.CreateElement("News");
//將節(jié)點(diǎn)加到指定節(jié)點(diǎn)下,作為其子節(jié)點(diǎn)
root.AppendChild(node);
//將節(jié)點(diǎn)加到指定節(jié)點(diǎn)下某個(gè)子節(jié)點(diǎn)前
root.InsertBefore(node,root.ChildeNodes[i]);
//為指定節(jié)點(diǎn)的新建屬性并賦值
node.SetAttribute("id","11111");
//為指定節(jié)點(diǎn)添加子節(jié)點(diǎn)
root.AppendChild(node);
//獲取指定節(jié)點(diǎn)的指定屬性值
string id=node.Attributes["id"].Value;
//獲取指定節(jié)點(diǎn)中的文本
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"));
感謝各位的閱讀!關(guān)于“.net怎么操作xml”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!