XML可擴展標記語言,標準通用標記語言的子集,是一種用於標記電子文件使其具備結構性的標記語言。spa
什麼是XML,學他有什麼用?xml
優勢:容易讀懂,格式標準任何語言都內置了XML分析引擎,不用單獨進行文件分析引擎得編寫。對象
XML語法規範:標籤(Tag)、嵌套(Nest)、屬性。標籤要閉合,屬性值要用」」包圍,標籤能夠相互嵌套。utf-8
大小寫敏感。文檔
XML樹,父節點、子節點、兄弟節點。get
寫入一個XML文件博客
//一、建立一個XML文檔對象string
XmlDocument doc = new XmlDocument();it
//二、建立第一行描述信息io
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");
//將子節點book1添加到根節點下
books.AppendChild(book1);
//七、給book1添加子節點
XmlElement bookName1 = doc.CreateElement("BookName");
//八、設置標籤中顯示的文本
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 (加羣備註:博客園看到的)