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)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement books = doc.CreateElement("Books");
doc.AppendChild(books);
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(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);
XmlElement book2 = doc.CreateElement("Book");
books.AppendChild(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
{
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 customertest = doc.CreateElement("CustomerLabel");
customertest.InnerXml = "<p>我是一个p标签</p>";
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();
}
}
}