Share simple methods and instructions for operating xml in .net

高洛峰
Release: 2017-03-12 15:55:59
Original
1619 people have browsed it

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"));
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template