XML增删改查

//增加节点;

 

 protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        string mappath = Server.MapPath("xml/bolgbooks.xml");
        xdoc.Load(mappath);

        XmlNode root= xdoc.SelectSingleNode("bookstore"); //找到bookstore的跟节点;
        XmlElement xe =xdoc .CreateElement("book");   //创建一个book的节点;
        xe.SetAttribute("genre", "张三");    //创建book的属性;
        xe.SetAttribute("ISBN", "123456");

        XmlElement title = xdoc.CreateElement("title"); //把标题给加进来
        title.InnerText = "C#入门宝典";
        xe.AppendChild(title);

        XmlElement author = xdoc.CreateElement("author");
        author.InnerText = "mike";
        xe.AppendChild(author);

        XmlElement price = xdoc.CreateElement("price");
        price.InnerText = "100";
        xe.AppendChild(price);

        root.AppendChild(xe);     //把book节点添加到bookstore的跟节点;
        xdoc.Save(mappath);
        Banding();
    }
    public void Banding()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("xml/bolgbooks.xml"));
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

 

 

//删除节点

 

  protected void Button2_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        string mappath = Server.MapPath("xml/bolgbooks.xml");
        xdoc.Load(mappath);

        XmlNodeList xmllist = xdoc.SelectSingleNode("bookstore").ChildNodes;

        foreach (XmlNode  xn  in xmllist)
        {
            XmlElement xe =(XmlElement)xn;
            if (xe.GetAttribute("ISBN")=="123")
            {
                xe.RemoveAll();
                break;
            }
        }
        xdoc.Save(mappath);
        Banding();
    }

 

//修改节点

 

   protected void Button3_Click(object sender, EventArgs e)
    {
        string file = Server.MapPath("xml/bolgbooks.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(file);          //把整个的xml加到内存里面;

        string isbn = "123456";

        XmlNode root = doc.DocumentElement;
        foreach (XmlNode node in root.ChildNodes)
        {
            if (node.Attributes["ISBN"].Value == isbn)
            {
                XmlNode anthor = node.SelectSingleNode("./author");
                anthor.InnerText = "王五";
                break;
            }
        }
        doc.Save(file);
        Banding();


    }

 

//查找节点

 

 protected void Button4_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        string mappath = Server.MapPath("xml/bolgbooks.xml");
        xdoc.Load(mappath);

        XmlNode root = xdoc.SelectSingleNode("bookstore"); //找到bookstore的跟节点;
        StringBuilder s = new StringBuilder();
        StringBuilder s1 = new StringBuilder();
        XmlNodeList xlist = root.ChildNodes;
        foreach (XmlNode  node in xlist)
        {
            XmlElement xe = (XmlElement)node;
            s1.Append(xe.GetAttribute("ISBN")+"<br>");
            s1.Append(xe.GetAttribute("genre"));
            XmlNodeList xn = xe.ChildNodes;
            foreach (XmlNode xn2 in xn)
            {
                s.Append(xn2.InnerText+"<br>");
            }
        }
        Label1.Text = s.ToString() ;
        Label2.Text = s1.ToString();
    }

你可能感兴趣的:(xml)