XML是yange区分大小写的。
XML标签也是成对出现的。
创建XML文档
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _01创建XML
{
class Program
{
static void Main(string[] args)
{
//通过代码来创建XML文档
//1、引入命名空间
//2、创建XML文档对象
XmlDocument doc = new XmlDocument();
//3、创建第一行描述信息,并且添加到doc文档中
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//4、创建根节点
XmlElement books = doc.CreateElement("Books");
//将根节点添加到文档中
doc.AppendChild(books);
//5、给根节点Books创建子节点
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 = "三国";
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();
}
}
}
查看创建的XML
结果
创建带属性的XML文档
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 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");
//给节点添加属性
orderItem1.SetAttribute("Name", "码表");
orderItem1.SetAttribute("Count", "10");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
//给节点添加属性
orderItem2.SetAttribute("Name", "iphone8");
orderItem2.SetAttribute("Count", "100");
items.AppendChild(orderItem2);
doc.Save("Order.xml");
Console.ReadKey();
}
}
}
结果
原有的XML追加XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace _03追加XML
{
class Program
{
static void Main(string[] args)
{
//追加XML文档
XmlDocument doc = new XmlDocument();
XmlElement books;
if (File.Exists("Books.xml"))
{
//如果文件存在,加载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);
}
//5、给根节点Books创建子节点
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");
//获得根节点
XmlElement books = doc.DocumentElement;
//获得子节点
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();
读取结果
删除节点
//删除节点
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNode xn = doc.SelectSingleNode("/Order/Items");
xn.RemoveAll();
doc.Save("Order.xml");
原有XML文档
运行结果
网友评论