html页面
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="book1">
<price>¥20</price>
<press>中信出版社</press>
</book>
<book id="book2">
<price>¥10</price>
<name>周恩来记事</name>
<press>中信出版社</press>
</book>
<book id="book3">
<price>¥30</price>
<name>毛泽东传</name>
<press>中信出版社</press>
</book>
<book id="book4">
<price>¥202</price>
<name>张大千花鸟图</name>
<press>中信出版社</press>
</book>
<book id="book5">
<price>¥24</price>
<name>清明上河图</name>
<press>中信出版社</press>
</book>
</books>
后台代码
/// <summary>
/// 创建/// </summary>
public void XmlCreate(){
string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "Document\\XMLFile1.xml";
XmlDocument xml = new XmlDocument();
xml.Load(xmlPath);//读取文件
XmlElement root = xml.DocumentElement;//获取根节点
//添加 节点
XmlElement node = xml.CreateElement("Student");
//节点属性 赋值
node.SetAttribute("Name", "小明");
node.SetAttribute("Age", "22");
node.SetAttribute("Sex", "男");
root.AppendChild(node);
XmlElement node1 = xml.CreateElement("book");
node1.SetAttribute("id", "book1");
XmlElement theElemName = xml.CreateElement("name");
theElemName.InnerText = "哈利波特22";
XmlElement theElem = xml.CreateElement("price");
theElem.InnerText = "¥20";
XmlElement theElemPress = xml.CreateElement("press");
theElemPress.InnerText = "中信出版社";
node1.AppendChild(theElem);
node1.AppendChild(theElemName);
node1.AppendChild(theElemPress);
root.AppendChild(node1);
xml.Save(xmlPath);//保存 xml文件
//xmltext = root.ToString();
Response.Write("成功");
}
/// <summary>
/// 查询
/// </summary>
public void XmlSelect() {
string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "Document\\XMLFile1.xml";
XmlDocument xml = new XmlDocument();
xml.Load(xmlPath);//读取文件
string text="";
XmlElement root = xml.DocumentElement;//获取根节点
//xmltext = root.InnerText.ToString();
var rootChil=root.ChildNodes.Item(1).ChildNodes.Count;
for (int i = 0; i <root.ChildNodes.Item(1).ChildNodes.Count; i++)
{
text+=root.ChildNodes.Item(1).ChildNodes.Item(i).FirstChild.InnerText;
}
this.Text1.Value = text;
Response.Write(xmltext);
}
/// <summary>
/// 删除
/// </summary>
public void XmlDel() {
string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "Document\\XMLFile1.xml";//文件路径
XmlDocument xml = new XmlDocument();
xml.Load(xmlPath);//读取文件
XmlElement root = xml.DocumentElement;//获取根节点
XmlNodeList rootChil = root.ChildNodes;//获取子节点
foreach (XmlNode xn in rootChil)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("id") == "student")//节点属性值条件比对
{
xn.ParentNode.RemoveChild(xn);
// xn.RemoveAll();
}
}
xml.Save(xmlPath);
//xmltext = root.ToString();
}
/// <summary>
/// 修改
/// </summary>
public void XmlUpdate()
{
string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "Document\\XMLFile1.xml";//文件路径
XmlDocument xml = new XmlDocument();
xml.Load(xmlPath);//读取文件
XmlElement root = xml.DocumentElement;//获取根节点
XmlNodeList rootChil = root.ChildNodes;//获取子节点
foreach (XmlNode xn in rootChil)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("id") == "student")//节点属性值条件比对
{
xe.SetAttribute("id", "stu");
}
}
xml.Save(xmlPath);
}