Home > Article > Backend Development > Share simple methods and instructions for operating xml in .net
Share the simple methods and instructions for operating xml on .net. Friends who need it can refer to it
The code is as follows:
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"));
The above is the detailed content of Share simple methods and instructions for operating xml in .net. For more information, please follow other related articles on the PHP Chinese website!