C#对XML文件的删除/添加/修改

Posted on 2007-04-02 23:26  零度海洋  阅读(696)  评论(0)    收藏  举报

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class ReadXml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Sodetail_edit(3);

    }


    public void CreateXml() {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec;
        dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); //add xml declaration
        doc.AppendChild(dec);

        XmlElement So = doc.CreateElement("So");  //add xml root element
        doc.AppendChild(So);


        XmlElement Customer = doc.CreateElement("Customer"); // Add Customer Information

        XmlElement address = doc.CreateElement("address");
                   address.InnerText = "hubei xg";
        XmlElement tel=doc.CreateElement("tel");
                    tel.InnerText="13590190254";
         XmlElement fax=doc.CreateElement("fax");
                    fax.InnerText="674566464";
         XmlElement company=doc.CreateElement("company");

                    company.InnerText = "promise";
                    Customer.AppendChild(address);
                    Customer.AppendChild(tel);
                    Customer.AppendChild(fax);
                    Customer.AppendChild(company);
        //------------------------------------------------------------------------------------------------
        XmlElement Delivery = doc.CreateElement("Delivery"); // Add Delivery Information
        XmlElement post_address = doc.CreateElement("address");
        post_address.InnerText = "hubei xg";
        XmlElement post_tel = doc.CreateElement("tel");
        post_tel.InnerText = "13590190254";
        XmlElement post_fax = doc.CreateElement("fax");
        post_fax.InnerText = "674566464";
        XmlElement post_company = doc.CreateElement("company");

        post_company.InnerText = "promise";
        Delivery.AppendChild(post_address);
        Delivery.AppendChild(post_tel);
        Delivery.AppendChild(post_fax);
        Delivery.AppendChild(post_company);
        //------------------------------------------------------------------------------------------------
        XmlElement SoDetail = doc.CreateElement("SoDetail");  // Add SoDetail Information
        SoDetail.SetAttribute("Id", "1");
        XmlElement printnum = doc.CreateElement("printnum");
        printnum.InnerText = "1000";
        XmlElement price = doc.CreateElement("price");
        price.InnerText = "200$";
        SoDetail.AppendChild(printnum);
        SoDetail.AppendChild(price);

        //-------------------------------------------------------------------------------------------------
        So.AppendChild(Customer);//Add element to root
        So.AppendChild(Delivery);
        So.AppendChild(SoDetail);


        doc.Save(Server.MapPath("bbs.xml"));

        //for (int i = 0; i < 4; i++)
        //{
        //    XmlNode employ = doc.SelectSingleNode("Employees");

        //    XmlElement subelement = doc.CreateElement("node");
        //    subelement.SetAttribute("key", 4);


        //}
   
   
    }

    public void Sodetail_add() { // Add a sodeatil to so

        XmlDocument doc = new XmlDocument();
         doc.Load(Server.MapPath("bbs.xml"));
         XmlNode So = doc.SelectSingleNode("So");

         XmlElement SoDetail = doc.CreateElement("SoDetail");  // Add SoDetail Information
         SoDetail.SetAttribute("Id", "3");
         XmlElement printnum = doc.CreateElement("printnum");
         printnum.InnerText = "6000";
         XmlElement price = doc.CreateElement("price");
         price.InnerText = "2080$";
         SoDetail.AppendChild(printnum);
         SoDetail.AppendChild(price);
         So.AppendChild(SoDetail);
         doc.Save(Server.MapPath("bbs.xml"));
   
    }

    public void Sodetail_delete(int index) {

        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("bbs.xml"));
        XmlNode root=doc.SelectSingleNode("So");
        XmlNodeList list = doc.SelectNodes("/So/SoDetail");
        for (int i = 0; i < list.Count; i++) {
            XmlElement CurNode = (XmlElement)list.Item(i);
            if (CurNode.GetAttribute("Id") == index.ToString())
            { //Find Node
                   // doc.RemoveChild(CurNode);
                root.RemoveChild(CurNode);
              
            }             
        }
      
         doc.Save(Server.MapPath("bbs.xml"));
   
    }

    public void Sodetail_edit(int index)  // modify
    {

        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("bbs.xml"));
      
        XmlNodeList list = doc.SelectNodes("/So/SoDetail");
        for (int i = 0; i < list.Count; i++)
        {
            XmlElement CurNode = (XmlElement)list.Item(i);
            if (CurNode.GetAttribute("Id") == index.ToString())
            {
                foreach (XmlNode subnode in CurNode) {

                    if (subnode.Name == "price") {

                        subnode.InnerText = "500$";
                    }
                }

            }
        }

        doc.Save(Server.MapPath("bbs.xml"));
   
    }

}

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3