.net读写xml

时间:2021-03-04 12:24:05

XML文件

<?xml version="1.0" encoding="utf-8"?>
<book>
<title>webapi入门</title>
<author>李白</author>
<price></price>
</book>

读XML

XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("/App_Data/data.xml"));
XmlElement root = xdoc.DocumentElement; //获取根节点
Response.Write(root.GetElementsByTagName("price")[].InnerText);

写XML

XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("/App_Data/data.xml"));
XmlElement root = xdoc.DocumentElement; //获取根节点
root.GetElementsByTagName("price")[].InnerText = "";
xdoc.Save(Server.MapPath("/App_Data/data.xml"));