1、创建普通的XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _03创建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 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("Des");
des1.InnerText = "好看,不解释!";
book1.AppendChild(des1);
doc.Save("books.xml");
Console.WriteLine("保存成功!");
Console.Read();
}
}
}
以下是执行后的结果:
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<Name>金瓶梅</Name>
<Price>10</Price>
<Des>好看,不解释!</Des>
</Book>
</Books>
2、向XML中追加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
namespace _03创建XML
{
class 追加XML
{
public static void ZhuiJiaXml()
{
//创建一个xml文档对象
XmlDocument doc = new XmlDocument();
//声明根节点
XmlElement books;
//判断xml文件是否存在
if (File.Exists("Books.xml"))
{
//如果存在,就进行加载
doc.Load("Books.xml");
//将根节点获取到
books = doc.DocumentElement;
}
else
{
//如果不存在,就进行创建
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
books = doc.CreateElement("Books");
doc.AppendChild(books);
}
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(book1);
XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "C#图解教程";
book1.AppendChild(name1);
XmlElement price1 = doc.CreateElement("Price");
price1.InnerText = "100";
book1.AppendChild(price1);
doc.Save("Books.xml");
Console.WriteLine("保存成功!");
Console.Read();
}
}
}
执行后:
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<Name>金瓶梅</Name>
<Price>10</Price>
<Des>好看,不解释!</Des>
</Book>
<Book>
<Name>C#图解教程</Name>
<Price>100</Price>
</Book>
</Books>
3、创建带属性的XML文档
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _03创建XML
{
class 创建带属性的xml
{
public static void CreateAppendShu()
{
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 orderNumber = doc.CreateElement("OrderNumber");
orderNumber.InnerText = "10000001";
order.AppendChild(orderNumber);
XmlElement Items = doc.CreateElement("Items");
order.AppendChild(Items);
XmlElement orderItem1 = doc.CreateElement("OrderItem");
//这里是重点!要调用一个SetAttribute方法
orderItem1.SetAttribute("Name", "码表");
orderItem1.SetAttribute("Count", "2");
Items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
orderItem2.SetAttribute("Name", "雨衣");
orderItem2.SetAttribute("Count", "40");
Items.AppendChild(orderItem2);
XmlElement orderItem3 = doc.CreateElement("OrderItem");
orderItem3.SetAttribute("Name", "手套");
orderItem3.SetAttribute("Count", "200000000");
Items.AppendChild(orderItem3);
doc.Save("Order.xml");
Console.WriteLine("保存成功!!");
}
}
}
执行后:
<?xml version="1.0" encoding="utf-8"?>
<Order>
<CustomerName>刘洋</CustomerName>
<OrderNumber>10000001</OrderNumber>
<Items>
<OrderItem Name="码表" Count="2" />
<OrderItem Name="雨衣" Count="40" />
<OrderItem Name="手套" Count="200000000" />
</Items>
</Order>
4、读取xml文档
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _03创建XML
{
class 读取xml
{
//读取普通的xml文档
public static void ReadXml()
{
XmlDocument doc = new XmlDocument();
//加载xml文档
doc.Load("Books.xml");
//获取根节点
XmlElement books = doc.DocumentElement;
//获取根节点下所有子节点
XmlNodeList xnl = books.ChildNodes;
foreach (XmlNode item in xnl)
{
Console.WriteLine(item.InnerText);
}
Console.Read();
}
//读取带有属性的xml文档
public static void ReadAttributeXml()
{
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
//指定到一个节点下,将那个节点的所有子节点赋给xnl
XmlNodeList xnl= doc.SelectNodes("/Order/Items/OrderItem");
foreach (XmlNode item in xnl)
{
Console.WriteLine(item.Attributes["Name"].Value);
Console.WriteLine(item.Attributes["Count"].Value);
}
Console.Read();
}
}
}
5、删除XML节点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _03创建XML
{
class 删除xml节点
{
public static void DeleteXmlNode()
{
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNode xn= doc.SelectSingleNode("/Order/Items");
xn.RemoveAll();
doc.Save("Order.xml");
Console.WriteLine("删除成功!");
}
}
}