using System;
using System.Xml;
namespace _02添加带有属性的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 customerName1 = doc.CreateElement("CustomerName");
customerName1.InnerText = "朱茵";
order.AppendChild(customerName1);
XmlElement orderName1 = doc.CreateElement("OrderNumber");
orderName1.InnerText = "6500";
order.AppendChild(orderName1);
XmlElement items = doc.CreateElement("Items");
order.AppendChild(items);
XmlElement orderItem1 = doc.CreateElement("OrderItem");
orderItem1.SetAttribute("Name", "风衣");
orderItem1.SetAttribute("Count", "1500");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
orderItem2.SetAttribute("Name", "手链");
orderItem1.SetAttribute("Count", "5000");
items.AppendChild(orderItem2);
doc.Save("Order.xml");
Console.WriteLine("保存生效");
Console.ReadKey();
}
}
}
得到的.xml文件为Oracle.xml
<?xml version="1.0" encoding="utf-8"?>
<Order>
<CustomerName>朱茵</CustomerName>
<OrderNumber>6500</OrderNumber>
<Items>
<OrderItem Name="风衣" Count="1500" />
<OrderItem Name="手链" Count="5000" />
</Items>
</Order>