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("刪除成功!");
}
}
}