用c#生成 XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XML_2021_11_24
{
    class Program
    {
        static void Main(string[] args)
        {
            //XML 可扩展的标记语言 ,存储数据
            //注意:1 XML严格区分大小写
            //2 XML的标签成对出现
            //3 XML 有且只有一个根节点
            //通过代码创建XML文档
            //1,引用命名空间

            //2,创建XML文档对象
            XmlDocument doc = new XmlDocument();

            //3,创建第一个行描述信息, 并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            //4,创建根节点,并将根节点加入到文档中
            XmlElement books =  doc.CreateElement("Books");
            doc.AppendChild(books);

            //5,给根节点Books创建子节点, 并将book1加入根节点
            XmlElement book1 = doc.CreateElement("Book");
            books.AppendChild(book1);

            //6, 给Book1添加子节点
            XmlElement name1 = doc.CreateElement("Name");
            name1.InnerText = "水浒传";
            book1.AppendChild(name1);

            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerText = "10元";
            book1.AppendChild(price1);

            XmlElement des1 = doc.CreateElement("Description");
            des1.InnerText = "好看,不解释";
            book1.AppendChild(des1);

            //7,创建book2
            XmlElement book2 = doc.CreateElement("Book");
            books.AppendChild(book2);

            //8, 给book2添加子节点
            XmlElement name2 = doc.CreateElement("Name");
            name2.InnerText = "西游记";
            book2.AppendChild(name2);

            XmlElement price2 = doc.CreateElement("Price");
            price2.InnerText = "20元";
            book2.AppendChild(price2);

            XmlElement des2 = doc.CreateElement("Description");
            des2.InnerText = "四个和尚一匹马";
            book2.AppendChild(des2);


            doc.Save("Books.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 带属性的XML //该看P249
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();

            //3,创建第一个行描述信息, 并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            XmlElement order = doc.CreateElement("Order"); //注意,直接使用了方法
            doc.AppendChild(order); //给doc添加的为描述/ 根节点

            XmlElement customertest = doc.CreateElement("CustomerLabel");
            customertest.InnerXml = "<p>我是一个p标签</p>"; //使用标签时候用InnerXml
            order.AppendChild(customertest);

            XmlElement customerName = doc.CreateElement("CustomerName");
            customerName.InnerText = "孙悟空";
            order.AppendChild(customerName);

            XmlElement customerNum = doc.CreateElement("CustomerNum");
            customerNum.InnerText = "1000001";
            order.AppendChild(customerNum);

            XmlElement items = doc.CreateElement("Items");
            order.AppendChild(items);

            XmlElement orderItem1 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem1.SetAttribute("Name","金箍棒");
            orderItem1.SetAttribute("Count", "10");
            items.AppendChild(orderItem1);


            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem2.SetAttribute("Name", "筋斗云");
            orderItem2.SetAttribute("Count", "2");
            items.AppendChild(orderItem2);


            XmlElement orderItem3 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem3.SetAttribute("Name", "火云刀");
            orderItem3.SetAttribute("Count", "2");
            items.AppendChild(orderItem3);

            doc.Save("Order.xml");

            Console.WriteLine("保存成功");
            Console.ReadKey();

        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值