用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标签

"
; //使用标签时候用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(); } } }

你可能感兴趣的:(c#,.net,visual,studio)