XML
XML可扩展的标记语言,存储数据,轻型数据库;HTML显示数据
注意:严格区分大小写,标签成对出现,有且只有有一个根节点
节点:标签就是节点
元素:所有的内容都是元素,元素包含节点
1、创建XML文档
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)
{
//通过代码创建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");
//将Book添加到根节点
books.AppendChild(book1);
//6、给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);
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 = "80";
book2.AppendChild(price2);
XmlElement des2 = doc.CreateElement("Des");
des2.InnerText = "英雄";
book2.AppendChild(des2);
doc.Save("Books.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
}
}
}
2、创建带属性的XML文档
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 customerName = doc.CreateElement("CustomerName");
customerName.InnerText = "你好";
order.AppendChild(customerName);
XmlElement customerNumber = doc.CreateElement("customerNumber");
customerNumber.InnerText = "1000001";
order.AppendChild(customerNumber);
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", "10");
items.AppendChild(orderItem2);
XmlElement orderItem3 = doc.CreateElement("orderItem");
//给节点添加属性
orderItem3.SetAttribute("Name", "码表");
orderItem3.SetAttribute("Count", "10");
items.AppendChild(orderItem3);
doc.Save("Order.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
}
}
}
3、读取XML内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ReadXML
{
class Program
{
static void Main(string[] args)
{
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.ReadKey();
}
}
}
4、读取xml属性值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Readxmlattribute
{
class Program
{
static void Main(string[] args)
{
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();
}
}
}
5、删除节点的值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace deletexmlnode
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNode xn = doc.SelectSingleNode("/Order/Items");
xn.RemoveAll();//删除该节点的内容
Console.WriteLine("删除成功");
Console.ReadKey();
}
}
}