XML是yange區(qū)分大小寫(xiě)的靶壮。
XML標(biāo)簽也是成對(duì)出現(xiàn)的种蝶。
創(chuàng)建XML文檔
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _01創(chuàng)建XML
{
class Program
{
static void Main(string[] args)
{
//通過(guò)代碼來(lái)創(chuàng)建XML文檔
//1、引入命名空間
//2靡羡、創(chuàng)建XML文檔對(duì)象
XmlDocument doc = new XmlDocument();
//3系洛、創(chuàng)建第一行描述信息,并且添加到doc文檔中
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//4略步、創(chuàng)建根節(jié)點(diǎn)
XmlElement books = doc.CreateElement("Books");
//將根節(jié)點(diǎn)添加到文檔中
doc.AppendChild(books);
//5描扯、給根節(jié)點(diǎn)Books創(chuàng)建子節(jié)點(diǎn)
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(book1);
XmlElement book1Name = doc.CreateElement("Name");
book1Name.InnerText = "紅樓";
book1.AppendChild(book1Name);
XmlElement book1Price = doc.CreateElement("Price");
book1Price.InnerText = "100";
book1.AppendChild(book1Price);
XmlElement book1Des = doc.CreateElement("Des");
book1Des.InnerText = "石頭記";
book1.AppendChild(book1Des);
XmlElement book2 = doc.CreateElement("Book");
books.AppendChild(book2);
XmlElement book2Name = doc.CreateElement("Name");
book2Name.InnerText = "三國(guó)";
book2.AppendChild(book2Name);
XmlElement book2Price = doc.CreateElement("Price");
book2Price.InnerText = "190";
book2.AppendChild(book2Price);
XmlElement book2Des = doc.CreateElement("Des");
book2Des.InnerText = "合久必分";
book2.AppendChild(book2Des);
doc.Save("Books.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
}
}
}
創(chuàng)建帶屬性的XML文檔
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _02創(chuàng)建帶屬性的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.InnerXml = "小明";
order.AppendChild(customerName);
XmlElement customerName2 = doc.CreateElement("CustomerName");
customerName2.InnerText = "大白";
order.AppendChild(customerName2);
XmlElement items = doc.CreateElement("Items");
order.AppendChild(items);
XmlElement orderItem1 = doc.CreateElement("OrderItem");
//給節(jié)點(diǎn)添加屬性
orderItem1.SetAttribute("Name", "碼表");
orderItem1.SetAttribute("Count", "10");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
//給節(jié)點(diǎn)添加屬性
orderItem2.SetAttribute("Name", "iphone8");
orderItem2.SetAttribute("Count", "100");
items.AppendChild(orderItem2);
doc.Save("Order.xml");
Console.ReadKey();
}
}
}
追加X(jué)ML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace _03追加X(jué)ML
{
class Program
{
static void Main(string[] args)
{
//追加X(jué)ML文檔
XmlDocument doc = new XmlDocument();
XmlElement books;
if (File.Exists("Books.xml"))
{
//如果文件存在,加載XML
doc.Load("Books.xml");
//獲得文件的根節(jié)點(diǎn)
books = doc.DocumentElement;
}
else {
//如果文件不存在
//創(chuàng)建第一行
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//創(chuàng)建根節(jié)點(diǎn)
books = doc.CreateElement("Books");
doc.AppendChild(books);
}
//5趟薄、給根節(jié)點(diǎn)Books創(chuàng)建子節(jié)點(diǎn)
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(book1);
XmlElement book1Name = doc.CreateElement("Name");
book1Name.InnerText = "西游記";
book1.AppendChild(book1Name);
XmlElement book1Price = doc.CreateElement("Price");
book1Price.InnerText = "100";
book1.AppendChild(book1Price);
XmlElement book1Des = doc.CreateElement("Des");
book1Des.InnerText = "孫猴子";
book1.AppendChild(book1Des);
doc.Save("Books.xml");
}
}
}
讀取XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _04讀取XML
{
class Program
{
static void Main(string[] args)
{
//加載要讀取的XML
XmlDocument doc = new XmlDocument();
doc.Load("BookS.xml");
//獲得根節(jié)點(diǎn)
XmlElement books = doc.DocumentElement;
//獲得子節(jié)點(diǎn)
XmlNodeList xnl = books.ChildNodes;
foreach (XmlNode item in xnl)
{
Console.WriteLine(item.InnerText);
}
Console.ReadKey();
}
}
}
讀取帶屬性的XML
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");
foreach (XmlNode node in xnl)
{
Console.WriteLine(node.Attributes["Name"].Value);
Console.WriteLine(node.Attributes["Count"].Value);
}
Console.ReadKey();
刪除節(jié)點(diǎn)
//刪除節(jié)點(diǎn)
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNode xn = doc.SelectSingleNode("/Order/Items");
xn.RemoveAll();
doc.Save("Order.xml");