XML的增删改查

时间:2022-10-20 22:08:54
 

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    private string file = HttpContext.Current.Server.MapPath("bookstore.xml");

    private XmlDocument doc = new XmlDocument();

    private StringBuilder sb = new StringBuilder();

   

    public void Page_Load(Object sender, EventArgs e)

    {

        if (!IsPostBack)

            ReadXml();

    }

    //显示所有数据

    private void ReadXml()

    {

        doc.Load(file);

        XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;

       

        foreach (XmlNode node in nodeList)

        {

            XmlElement element = (XmlElement)node;

            sb.Append(element.GetAttribute("genre") + "<br />");    //显示属性值

            sb.Append(element.GetAttribute("ISBN") + "<br />");

 

            XmlNodeList nextNodeList = element.ChildNodes;

            foreach (XmlNode nextNode in nextNodeList)

            {

                sb.Append(nextNode.InnerText + "<br />");          //显示子节点文本

            }

        }

 

        ltXml.Text = sb.ToString();

    }

    //增加

    protected void btnAdd_Click(object sender, EventArgs e)

    {

        doc.Load(file);

        XmlNode node = doc.SelectSingleNode("bookstore");           //查找<bookstore>节点

 

        XmlElement element = doc.CreateElement("book");             //创建一个<book>节点

        element.SetAttribute("genre", "Text");                      //设置该节点genre属性

        element.SetAttribute("ISBN", "7-8-98");                     //设置该节点ISBN属性

       

        XmlElement titleElement = doc.CreateElement("title");

        titleElement.InnerText = "TextTitle";                       //设置文本

        element.AppendChild(titleElement);

       

        XmlElement authorElement = doc.CreateElement("author");

        authorElement.InnerText = "TextAuthor";

        element.AppendChild(authorElement);

       

        XmlElement priceElement = doc.CreateElement("price");

        priceElement.InnerText = "9.22";

        element.AppendChild(priceElement);

 

        node.AppendChild(element);                                  //添加到<bookstore>节点中

       

        doc.Save(file);

        ReadXml();

    }

    //修改

    protected void btnUpdate_Click(object sender, EventArgs e)

    {

        doc.Load(file);

        XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;

       

        foreach(XmlNode node in nodeList)

        {

            XmlElement element = (XmlElement)node;                  //将子节点类型转换为XmlElement类型

            if (element.GetAttribute("genre") == "Text")            //如果genre属性值为Text

            {

                element.SetAttribute("genre", "updateText");        //则修改该属性为updateText

               

                XmlNodeList nextNodeList = element.ChildNodes;      //继续获取element子节点的所有子节点

                foreach (XmlNode nextNode in nextNodeList)

                {

                    if (nextNode.Name == "author")

                    {

                        nextNode.InnerText = "亚胜";

                        break;

                    }

                }

                break;

            }

        }

       

        doc.Save(file);

        ReadXml();

    }

    //删除

    protected void btnDelete_Click(object sender, EventArgs e)

    {

        doc.Load(file);

        XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;

 

        foreach (XmlNode node in nodeList)

        {

            XmlElement element = (XmlElement)node;

            if (element.GetAttribute("genre") == "fantasy")

                element.RemoveAttribute("genre");                   //按名称移除属性

            else if (element.GetAttribute("genre") == "updateText")

                element.RemoveAll();                                //移除所有属性和子级

        }

       

        doc.Save(file);

        ReadXml();

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Xml增删改查</title>

</head>

<body>

    <form runat="server">

    <div>

        <asp:Literal ID="ltXml" runat="server"></asp:Literal><br />

        <asp:Button ID="btnAdd" runat="server" Text="AddXml" onclick="btnAdd_Click" />&nbsp;

        <asp:Button ID="btnUpdate" runat="server" Text="UpdateXml" onclick="btnUpdate_Click" />&nbsp;

        <asp:Button ID="btnDelete" runat="server" Text="DeleteXml" onclick="btnDelete_Click" />

    </div>

    </form>

</body>

</html>

 bookstore.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>

<bookstore>

     <book genre="fantasy" ISBN="2-3631-4">

         <title>Oberon's Legacy</title>

         <author>Corets, Eva</author>

         <price>5.95</price>

     </book>

</bookstore>