C# 生成 添加 修改 删除 XML节点操作

    XmlDocument xmldoc;
    XmlNode xmlnode;
    XmlElement xmlelem;
    protected void Page_Load(object sender, EventArgs e)
    {
        //创建文件和添加节点
        xmldoc = new XmlDocument();
        //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
        XmlDeclaration xmldecl;
        xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
        xmldoc.AppendChild(xmldecl);

        //加入一个根元素
        xmlelem = xmldoc.CreateElement("", "Employees", "");
        xmldoc.AppendChild(xmlelem);
        //加入另外一个元素
        for (int i = 1; i < 3; i++)
        {

            XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees>
            XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点
            xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性
            xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性

            XmlElement xesub1 = xmldoc.CreateElement("title");
            xesub1.InnerText = "CS从入门到精通";//设置文本节点
            xe1.AppendChild(xesub1);//添加到<Node>节点中

            XmlElement xesub2 = xmldoc.CreateElement("author");
            xesub2.InnerText = "候捷";
            xe1.AppendChild(xesub2);
            XmlElement xesub3 = xmldoc.CreateElement("price");
            xesub3.InnerText = "58.3";
            xe1.AppendChild(xesub3);

            root.AppendChild(xe1);//添加到<Employees>节点中
        }
        //保存创建好的XML文档
        xmldoc.Save(Server.MapPath("data.xml"));


        //添加节点
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));
        XmlNode roots = xmlDoc.SelectSingleNode("Employees");//查找<Employees>

        XmlElement xe2 = xmlDoc.CreateElement("Node");//创建一个<Node>节点
        xe2.SetAttribute("genre", "张三");//设置该节点genre属性
        xe2.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性

        XmlElement xesub6 = xmlDoc.CreateElement("title");
        xesub6.InnerText = "C#入门帮助";//设置文本节点
        xe2.AppendChild(xesub6);//添加到<Node>节点中
        XmlElement xesub4 = xmlDoc.CreateElement("author");
        xesub4.InnerText = "高手";
        xe2.AppendChild(xesub4);
        XmlElement xesub5 = xmlDoc.CreateElement("price");
        xesub5.InnerText = "158.3";
        xe2.AppendChild(xesub5);

        roots.AppendChild(xe2);//添加到<Employees>节点中
        xmlDoc.Save(Server.MapPath("data.xml"));



        //修改节点属性和内容
        xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));

        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

        foreach (XmlNode xn in nodeList)//遍历所有子节点
        {
            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
            if (xe.GetAttribute("genre") == "张三")//如果genre属性值为“张三”
            {
                xe.SetAttribute("genre", "update张三");//则修改该属性为“update张三”

                XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                foreach (XmlNode xn1 in nls)//遍历
                {
                    xe2 = (XmlElement)xn1;//转换类型
                    if (xe2.Name == "author")//如果找到
                    {
                        xe2.InnerText = "亚胜";//则修改
                    }
                }
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));//保存。

        //添加节点
        xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));

        nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

        foreach (XmlNode xn in nodeList)
        {
            XmlElement xe = (XmlElement)xn;
            xe.SetAttribute("test", "111111");

            XmlElement xesub = xmlDoc.CreateElement("flag");
            xesub.InnerText = "1";
            xe.AppendChild(xesub);
        }
        xmlDoc.Save(Server.MapPath("data.xml"));



        //删除属性和子节点
        xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));
        XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
        foreach (XmlNode xn in xnl)
        {
            XmlElement xe = (XmlElement)xn;
            xe.RemoveAttribute("genre");//删除genre属性

            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
            foreach (XmlNode xn1 in nls)//遍历
            {
                xe2 = (XmlElement)xn1;//转换类型
                if (xe2.Name == "flag")//如果找到
                {
                    xe.RemoveChild(xe2);//则删除
                }
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));

        //删除节点
        xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));
        XmlNode rootq = xmlDoc.SelectSingleNode("Employees");
        xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
        for (int i = 0; i < xnl.Count; i++)
        {
            XmlElement xe = (XmlElement)xnl.Item(i);
            if (xe.GetAttribute("ISBN") == "1-1111-10")
            {
                rootq.RemoveChild(xe);
                if (i < xnl.Count) i = i - 1;
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));


        //读取节点
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("data.xml"));
        //根据属性查找节点
        //XmlNode xns = doc.SelectSingleNode("/Employees/Node[@ISBN='2-3631-4']");
        //根据子节点内容
        XmlNode xns = doc.SelectSingleNode("Employees/Node/author[text()='候捷']");
        //根据多层节点查找
        //objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name")


        xnl = xns.ChildNodes;
        foreach (XmlNode xnf in xnl)
        {
           // XmlElement xe = (XmlElement)xnf;
            //Response.Write(xe.GetAttribute("ISBN"));//显示属性值

           // XmlNodeList xnf1 = xe.ChildNodes;
           // foreach (XmlNode xn2 in xnf1)
           // {
            Response.Write(xnf.Name + ":" + xnf.InnerText);//显示子节点点文本
            //}
            Response.Write("<br>");
        }

        //XmlNode objNode = doc.SelectSingleNode("Employees/Node[ISBN='2-3631-4']");


    }

你可能感兴趣的:(xml,C#)