创建带属性的XML文档

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

namespace 创建带属性的XML文档
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);

            XmlElement customerName = doc.CreateElement("CustomerName");
            customerName.InnerText = "肚子好饿";
            order.AppendChild(customerName);

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

            XmlElement orderItem1 = doc.CreateElement("orderItem");
            orderItem1.SetAttribute("Name", "炒粉干");
            orderItem1.SetAttribute("Count", "2");
            items.AppendChild(orderItem1);

            doc.Save("Order.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/blacop/p/5995084.html