在C#.net中如何操作XML(转)

在C#.net中如何操作XML

需要添加的命名空间:

using System.Xml;

几个公共对象:

XmlDocument xmldoc ;

XmlNode xmlnode ;

XmlElement xmlelem ;

第一步:

//创建到服务器同名目录下的xml文件:

        XmlDocument xdoc = new XmlDocument();

        //加入XML的声明部分<?xml version="1.0" encoding="gb2312" ?>

        XmlDeclaration xdecl = xdoc.CreateXmlDeclaration("1.0", "gb2312", null);

        xdoc.AppendChild(xdecl);

 

        //加入一根元素<Employees></Employees>

        XmlElement xelem = xdoc.CreateElement("Employees");

        xdoc.AppendChild(xelem);

 

        //向根元素中添加节点<Node></Node>

        for (int i = 1; i < 3; i++)

        {

            //取出根节点

            XmlNode root = xdoc.SelectSingleNode("Employees");

            //创建一个<Node>节点

            XmlElement xel = xdoc.CreateElement("Node");

            //设置节点的属性

            xel.SetAttribute("name", "张三疯");

            xel.SetAttribute("favor", "论剑");

 

            //创建节点<title>

            XmlElement xSub1 = xdoc.CreateElement("title");

            //设置节点中的文本内容

            xSub.InnerText = "太极功夫";

            //将<title>添加到<Node>中

            xel.AppendChild(xSub);

            //

            XmlElement xSub2 = xdoc.CreateElement("author");

            xSub2.InnerText = "三疯哥";

            xel.AppendChild(xSub2);

            XmlElement xSub3 = xdoc.CreateElement("price");

            xSub3.InnerText = "20.8";

            xel.AppendChild(xSub3);

 

            //将<Node>节点添加到根节点<Employees>中

            root.AppendChild(xel);

        }

 

        //保存文件

xdoc.Save(Server.MapPath("data.xml"));

//////////////////////////////////////////////////////////////////////////////////////

在服务器目录下生成了名为data.xml文件,内容如下所示:

<?xml version="1.0" encoding="gb2312"?>

<Employees>

  <Node name="张三疯" favor="论剑">

    <title>太极功夫</title>

    <author>三疯哥</author>

    <price>20.8</price>

  </Node>

  <Node name="张三疯" favor="论剑">

    <title>太极功夫</title>

    <author>三疯哥</author>

    <price>20.8</price>

  </Node>

</Employees>

 

第二步:向文档中添加一个新节点内容

//定义一个XML文档对象,加载XML文件

        XmlDocument xdoc = new XmlDocument();

        xdoc.Load(Server.MapPath("data.xml"));

 

        //取出文档中的根节点<Employees>

        XmlNode root = xdoc.SelectSingleNode("Employees");

        //创建一个<Node>节点

        XmlElement xel = xdoc.CreateElement("Node");

        //设置节点属性

        xel.SetAttribute("name", "如来");

        xel.SetAttribute("favor", "论棍");

 

        //定义子节点

        XmlElement xSub1 = xdoc.CreateElement("title");

        xSub1.InnerText= "狂疯棍法";

        xel.AppendChild(xSub1);

        XmlElement xSub2 = xdoc.CreateElement("author");

        xSub2.InnerText = "高手";

        xel.AppendChild(xSub2);

        XmlElement xSub3 = xdoc.CreateElement("price");

        xSub3.InnerText = "123.4";

        xel.AppendChild(xSub3);

 

        //将新节点添加到根节点中

        root.AppendChild(xel);

        //保存文档

        xdoc.Save(Server.MapPath("data.xml"));

 

//////////////////////////////////////////////////////////////////////////////////////

结果在原有内容中添加了一条新内容:

<?xml version="1.0" encoding="gb2312"?>

<Employees>

  <Node name="张三疯" favor="论剑">

    <title>太极功夫</title>

    <author>三疯哥</author>

    <price>20.8</price>

  </Node>

  <Node name="张三疯" favor="论剑">

    <title>太极功夫</title>

    <author>三疯哥</author>

    <price>20.8</price>

  </Node>

  <Node name="如来" favor="论棍">

    <title>狂疯棍法</title>

    <author>高手</author>

    <price>123.4</price>

  </Node>

</Employees>

 

第三步:修改节点及其属性的值

/载入XML

        XmlDocument xdoc = new XmlDocument();

        xdoc.Load(Server.MapPath("data.xml"));

 

        //获取<Employees>节点中的所有子节点

        XmlNodeList nodeList = xdoc.SelectSingleNode("Employees").ChildNodes;

 

        //遍历所有子节点

        foreach (XmlNode xn in nodeList)

        {

            //如果节点的属性值为张三疯

            if (xn.Attributes["name"].Value == "张三疯")

            {

                //则修改成为

                xn.Attributes["name"].Value = "张三丰";

                //继续遍历该节点

                foreach (XmlNode n in xn.ChildNodes)

                {

                    //如果节点的名称为author

                    if (n.Name == "author")

                    {

                        //则修改文本

                        n.InnerText = "张无忌";

                    }

                }

            }

        }

xdoc.Save(Server.MapPath("data.xml"));

//////////////////////////////////////////////////////////////////////////////////////
结果:将原来的所有结点的信息都修改了,xml的内容如下,
<?xml version="1.0" encoding="gb2312"?>

<Employees>

  <Node name="张三丰" favor="论剑">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

  </Node>

  <Node name="张三丰" favor="论剑">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

  </Node>

  <Node name="如来" favor="论棍">

    <title>狂疯棍法</title>

    <author>高手</author>

    <price>123.4</price>

  </Node>

</Employees>

 

4,修改结点(添加结点的属性和添加结点的自结点):

XmlDocument xdoc = new XmlDocument();

        xdoc.Load(Server.MapPath("data.xml"));

 

        //获取根节点外的所有子结点

        XmlNodeList nodeList = xdoc.SelectSingleNode("Employees").ChildNodes;

        //遍历

        foreach (XmlNode xn in nodeList)

        {

            //给点节添加一个新属性

            XmlElement xe = xn as XmlElement;

            xe.SetAttribute("test", "111111");

            //创建一个子节点

            XmlElement xeSub = xdoc.CreateElement("flag");

            xeSub.InnerText = "1";

            //将新子结点添加到元素中

            xe.AppendChild(xeSub);           

        }

        xdoc.Save(Server.MapPath("data.xml"));

//////////////////////////////////////////////////////////////////////////////////////
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下:

<?xml version="1.0" encoding="gb2312"?>

<Employees>

  <Node name="张三丰" favor="论剑" test="111111">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

    <flag>1</flag>

  </Node>

  <Node name="张三丰" favor="论剑" test="111111">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

    <flag>1</flag>

  </Node>

  <Node name="如来" favor="论棍" test="111111">

    <title>狂疯棍法</title>

    <author>高手</author>

    <price>123.4</price>

    <flag>1</flag>

  </Node>

</Employees>

 

5,删除结点中的某一个属性:

XmlDocument xdoc = new XmlDocument();

        xdoc.Load(Server.MapPath("data.xml"));

 

        XmlNodeList nodeList = xdoc.SelectSingleNode("Employees").ChildNodes;

        foreach (XmlNode xn in nodeList)

        {

            //删除一个属性favor

            XmlElement xe = xn as XmlElement;

            xe.RemoveAttribute("favor");

            foreach (XmlNode n in xn.ChildNodes)

            {

                XmlElement x2 = n as XmlElement;

                //如果找到名为flag的结点,则删除

                if (x2.Name == "flag")

                {

                    xn.RemoveChild(n);

                }

            }

        }

        xdoc.Save(Server.MapPath("data.xml"));

//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了结点的一个属性和结点的一个子结点,内容如下:

<?xml version="1.0" encoding="gb2312"?>

<Employees>

  <Node name="张三丰" test="111111">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

  </Node>

  <Node name="张三丰" test="111111">

    <title>太极功夫</title>

    <author>张无忌</author>

    <price>20.8</price>

  </Node>

  <Node name="如来" test="111111">

    <title>狂疯棍法</title>

    <author>高手</author>

    <price>123.4</price>

  </Node>

</Employees>

原文地址:https://www.cnblogs.com/tianya10319/p/2279886.html