.NET 基础 一步步 一幕幕[XML基础操作]
XML可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。
什么是XML,学他有什么用?
优点:容易读懂,格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎得编写。
XML语法规范:标签(Tag)、嵌套(Nest)、属性。标签要闭合,属性值要用””包围,标签可以相互嵌套。
大小写敏感。
XML树,父节点、子节点、兄弟节点。
写入一个XML文件
//1、创建一个XML文档对象
XmlDocument doc = new XmlDocument();
//2、创建第一行描述信息
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//3、将创建的第一行数据添加到文档中
doc.AppendChild(dec);
//4、给文档添加根节点
XmlElement books = doc.CreateElement("Books");
//5、将根节点添加给文档对象
doc.AppendChild(books);
//6、给根节点添加子节点
XmlElement book1 = doc.CreateElement("Book");
//将子节点book1添加到根节点下
books.AppendChild(book1);
//7、给book1添加子节点
XmlElement bookName1 = doc.CreateElement("BookName");
//8、设置标签中显示的文本
bookName1.InnerText = "水浒传";
book1.AppendChild(bookName1);
XmlElement author1 = doc.CreateElement("Author");
author1.InnerText = "<authorName>匿名</authorName>";
book1.AppendChild(author1);
XmlElement price1 = doc.CreateElement("Price");
price1.InnerXml = "<authorName>匿名</authorName>";
book1.AppendChild(price1);
XmlElement des1 = doc.CreateElement("Des");
des1.InnerXml = "好看,顶!~!!!!";
book1.AppendChild(des1);
Console.WriteLine("保存成功");
doc.Save("Book.xml");
演示结果:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<BookName>水浒传</BookName>
<Author>XXX</Author>
<Price>100RMB</Price>
<Des>好看!!经典!!!!顶!!!!</Des>
</Book>
<Book>
<BookName>三国演义</BookName>
<Author>XXX</Author>
<Price>200RMB</Price>
<Des>好看,顶!!!!</Des>
</Book>
</Books>
添加带属性的XML文档
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 orderNumber = doc.CreateElement("OrderNumber");
orderNumber.InnerXml = "10000";
order.AppendChild(orderNumber);
XmlElement items = doc.CreateElement("Items");
order.AppendChild(items);
XmlElement orderItem1 = doc.CreateElement("OrderItem");
orderItem1.SetAttribute("Name", "码表");
orderItem1.SetAttribute("Count", "100001");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
orderItem2.SetAttribute("Name", "雨衣");
orderItem2.SetAttribute("Count", "1");
items.AppendChild(orderItem2);
doc.Save("Order.xml");
文档对象模型
对象实体:
Public class Student
{
public int Age { get; set; }
public string Name { get; set; }
public int ID { get; set; }
public char Gender { get; set; }
}
List<Student> list = new List<Student>();
list.Add(new Student() { ID = 1, Name = "yhb", Gender = '男', Age = 30 });
list.Add(new Student() { ID = 2, Name = "wl", Gender = '女', Age = 20 });
list.Add(new Student() { ID = 3, Name = "刘德华", Gender = '男', Age = 50 });
list.Add(new Student() { ID = 4, Name = "张学友", Gender = '男', Age = 60 });
list.Add(new Student() { ID = 5, Name = "哥哥", Gender = '男', Age = -10 });
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement person = doc.CreateElement("Person");
doc.AppendChild(person);
//通过循环List集合,获得所有对象 以节点的形式添加到XML文档中
for (int i = 0; i < list.Count; i++)
{
XmlElement student = doc.CreateElement("Student");
student.SetAttribute("studentID", list[i].ID.ToString());
XmlElement name = doc.CreateElement("Name");
name.InnerXml = list[i].Name;
XmlElement age = doc.CreateElement("Age");
age.InnerXml = list[i].Age.ToString();
XmlElement gender = doc.CreateElement("Gender");
gender.InnerXml = list[i].Gender.ToString();
//添加
person.AppendChild(student);
student.AppendChild(name);
student.AppendChild(age);
student.AppendChild(gender);
}
doc.Save("Student.xml");
对xml文档实现增删改查
//XMLDocument
#region 对xml文档实现追加的需求
XmlDocument doc = new XmlDocument();
//首先判断xml文档是否存在 如果存在 则追加 否则创建一个
if (File.Exists("Student.xml"))
{
//加载进来
doc.Load("Student.xml");
//追加
//获得根节点 给根节点添加子节点
XmlElement person = doc.DocumentElement;
XmlElement student = doc.CreateElement("Student");
student.SetAttribute("studentID", "10");
person.AppendChild(student);
XmlElement name = doc.CreateElement("Name");
name.InnerXml = "我是新来哒";
student.AppendChild(name);
XmlElement age = doc.CreateElement("Age");
age.InnerXml = "18";
student.AppendChild(age);
XmlElement gender = doc.CreateElement("Gender");
gender.InnerXml = "女";
student.AppendChild(gender);
}
else
{
//不存在
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement person = doc.CreateElement("Person");
doc.AppendChild(person);
XmlElement student = doc.CreateElement("Student");
student.SetAttribute("studentID", "110");
person.AppendChild(student);
XmlElement name = doc.CreateElement("Name");
name.InnerXml = "张三三李思思";
student.AppendChild(name);
XmlElement age = doc.CreateElement("Age");
age.InnerXml = "28";
student.AppendChild(age);
XmlElement gender = doc.CreateElement("Gender");
gender.InnerXml = "男";
student.AppendChild(gender);
}
doc.Save("Student.xml");
Console.WriteLine("保存成功");
#endregion
#region 读取XML文档
XmlDocument doc = new XmlDocument();
doc.Load("OrDER.xml");
还是 先获得根节点
XmlElement order = doc.DocumentElement;
获得根节点下面的所有子节点
XmlNodeList xnl = order.ChildNodes;
foreach (XmlNode item in xnl)
{
Console.WriteLine(item.InnerText);
}
XmlElement items = order["Items"];
XmlNodeList xnl2 = items.ChildNodes;
foreach (XmlNode item in xnl2)
{
Console.WriteLine(item.Attributes["Name"].Value);
Console.WriteLine(item.Attributes["Count"].Value);
if (item.Attributes["Name"].Value == "手套")
{
item.Attributes["Count"].Value = "新来哒";
}
}
doc.Save("OrDER.xml");
#endregion
#region 使用XPath的方式来读取XML文件
XmlDocument doc = new XmlDocument();
doc.Load("order.xml");
//获得根节点
XmlElement order = doc.DocumentElement;
XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");
Console.WriteLine(xn.Attributes["Name"].Value);
xn.Attributes["Count"].Value = "woshi new";
doc.Save("order.xml");
Console.WriteLine("保存成功");
#endregion
XmlDocument doc = new XmlDocument();
doc.Load("order.xml");
//doc.RemoveAll();不行 根节点不允许删除
XmlElement order = doc.DocumentElement;
order.RemoveAll(); 移除根节点下的所有子节点
XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");
//让orderItem去删除属性
XmlNode orderItem = order.SelectSingleNode("/Order/Items/OrderItem");
//获得Items节点
XmlNode items = order["Items"];//order.SelectSingleNode("/Order/Items");
items.RemoveChild(xn); //移除当前节点
//orderItem.RemoveAttributeNode(xn.Attributes["Count"]);
xn.Attributes.RemoveNamedItem("Count");
doc.Save("order.xml");
Console.WriteLine("删除成功");
本篇就到这里了,最后打个小广告:QQ群: .NET 一步步一幕幕 群号:590170361 (加群备注:博客园看到的)