向空XML自己添加数据成XML格式

 

[WebMethod(Description = "向空XML自己添加数据")]
        public XmlDocument XD()
        {
            XmlDocument xmldoc = new XmlDocument();
            //加入XML的声明段落,<?xml version="1.0" encoding="utf-8"?>
            xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0","utf-8",null));
            //XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //xmldoc.AppendChild(xmldecl);

            //创建根元素
            XmlElement xmlelem = xmldoc.CreateElement("","Employees","");
            xmldoc.AppendChild(xmlelem);

            //设置根元素
            XmlNode root = xmldoc.SelectSingleNode("Employees");//查找

               //添加第一个子元素
            XmlElement xe_1 = xmldoc.CreateElement("Employees");
            xe_1.SetAttribute("Ename", "第一个人");
            xe_1.SetAttribute("Esex", "男");
            xe_1.SetAttribute("Eage", "17岁");
            root.AppendChild(xe_1);

            //添加第一个子元素的子元素
            XmlElement xe_1_1 = xmldoc.CreateElement("Node");
            xe_1_1.SetAttribute("BName", "英国");
            xe_1_1.SetAttribute("BSN", "Vec.1");
            XmlElement xe_1_2 = xmldoc.CreateElement("title");
            xe_1_2.InnerText = "C#学习";
            XmlElement xe_1_3 = xmldoc.CreateElement("Autor");
            xe_1_3.InnerText = "张三";
            XmlElement xe_1_4 = xmldoc.CreateElement("price");
            xe_1_4.InnerText = "65";
            xe_1.AppendChild(xe_1_1);
            xe_1.AppendChild(xe_1_2);
            xe_1.AppendChild(xe_1_3);
            xe_1.AppendChild(xe_1_4);

            //添加第二个子元素
            XmlElement xe_2 = xmldoc.CreateElement("Employees");
            xe_2.SetAttribute("Ename", "第二个人");
            xe_2.SetAttribute("Esex", "女");
            xe_2.SetAttribute("Eage", "17岁");
            root.AppendChild(xe_2);

            //添加第二个子元素的子元素
            XmlElement xe_2_1 = xmldoc.CreateElement("Node");
            xe_2_1.SetAttribute("BName", "英国");
            xe_2_1.SetAttribute("BSN", "Vec.2");
            XmlElement xe_2_2 = xmldoc.CreateElement("title");
            xe_2_2.InnerText = "ASP学习";
            XmlElement xe_2_3 = xmldoc.CreateElement("Autor");
            xe_2_3.InnerText = "李四";
            XmlElement xe_2_4 = xmldoc.CreateElement("price");
            xe_2_4.InnerText = "88";
            xe_2.AppendChild(xe_2_1);
            xe_2.AppendChild(xe_2_2);
            xe_2.AppendChild(xe_2_3);
            xe_2.AppendChild(xe_2_4);
            
            //返回xml
            return xmldoc;
        }
<?xml version="1.0" encoding="UTF-8"?>
-<Employees> -<Employees Eage="17岁" Esex="男" Ename="第一个人"> <Node BSN="Vec.1" BName="英国"/> <title>C#学习</title> <Autor>张三</Autor> <price>65</price> </Employees> -<Employees Eage="17岁" Esex="女" Ename="第二个人"> <Node BSN="Vec.2" BName="英国"/> <title>ASP学习</title> <Autor>李四</Autor> <price>88</price> </Employees> </Employees>



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值