XML(可扩展标记语言)

  • XML:eXtensible Markup Language
  • 什么是标记语言?什么是标记?
  • 标记(markup):文档中任何不想被打印输出的部分(不是真正的文档的内容,联想读书时做的“读书笔记”,在旁边写的注解等。)注解是注解,实际内容是实际内容。
  • 标记的作用:传递了关于文档本身以外的额外信息。比如:标记文档的某部分该如何显示,某部分是什么意思等。重在数据,标记只是为了说明数据的含义而已。
  • 常见的标记语言:SGML、HTML、XML
  • HTML与XML的区别与联系?
  • Xml作用与应用场合:xml数据存储,html数据展示。
  • 语法、是否有预定义标签、各自的作用与意义
  • 相关术语:标签、节点、根节点、元素、子元素、后代元素、属性、嵌套、命名空间、字符数据(CDATA)

XML 基础(extensible markup language)

  • 仕么是XML,学它有什么用。优点:容易读懂:格式标准任何语言都内置了XML分析引繁,不用单独进行文件分析引擎的编写。
  • Xml就是用一种格式化的方式来存储数据,并且是文本文件任何时候我们都能很容易的用记事本打开。与Excel、Word不一样。
  • 举例:分析出一个xml格式的文件、分析出一个Excel文件。net程序中的一些配置文件app.config、web.config文件都是xm文件。
  • Ofice2007的文件格式docx、xlsx、pptx都是xml.先改成rar文件然后解压。
  • XML语法规范:标签(Tag)、嵌套(Nest)、属性。标签要闭合,属性值要用​​""​​包围,标签可以互相嵌套
  • XML树,父节点、子节点、兄弟节点(siblings)
  • XML和HTML的区别:
  1. 有且只能有一个根元素。
  2. XML中元素必须关闭;
  3. XML中元素的属性值必须用引号;
  4. XML大小写敏感(CaseSenstive)
  • 符合XML规范的HTML叫做“符合XHTML标准”。开发的网站必须通过W3C验证。
  • xml编写完成以后可以用浏览器来查着,如果写错了浏览器会提示。如果明明没措,浏览器还是提示错娱,则可能是文件编码问题。

Linq To XML:写

  • 读写XML有很多技术:
  1. ​Dom [XmlDocument、XDocument]​​​(文档对象模型,将整个​​xml​​加载到内存中,然后操作);
  2. ​Sax​​​(事件驱动,​​.net​​​中使用​​XmlReader(XmlTextReader)​​​、​​XmlWriter(XmlTextWriter)​​ 来替代)等,还有高级的读写技术;
  3. ​XmlSerializer​​​(​​xml​​序列化,需要先定义类);
  4. ​Linq To XML(SystemXml.Linq)​​​,用到的就是​​Xdocument​​​,​​Xelement​​​等等,​​XmlSerializer​​​要求对每种不同的文件都定义一套类,很麻烦,而​​Linq To XML​​​则不需要单独创建类,当然更底层一些,代码比​​XmlSerializer​​​多,灵活性更高。​​SystemXml​​​下的类是2.0及之前操作​​xml​​​推荐的,现在很多入也仍然在用这个​​namespace​​​下的类,这个​​namespace​​​下的类和​​Linq To XML​​​非常相似,因此不用单独学核心类​​XElement​​​,一个​​XElement​​​表示一个元素,​​new XEiement("order")​​​,创建一个名学为​​Order​​​的标签,调用​​Add​​​增加子元素,也是​​XElement​​​对象,和​​TreeView​​一样。
  • 想得到字符串怎么办?​​ToString​
  • 调用​​XElement​​​的​​Save​​​方法将​​xml​​​内容保存在​​Writer​​中
  • 创建xml的时候可以用​​XDocument​​​也可以不用。(直接用​​XElement​​)

实例

C#.Net  XML_xml

▲ 界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace XML_Bzhan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
#region 通过编程的方式实现XML写入
// 1. 在内存中构建一个 Dom 对象
XmlDocument xmlDoc = new XmlDocument();
// 增加一个文档说明
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDeclaration);
// 为文档增加一个根元素
// 2.1 创建一个根元素
XmlElement rootElement = xmlDoc.CreateElement("school");
xmlDoc.AppendChild(rootElement);

// 为根元素增加子元素,接下来增加元素都要将子元素增加到rootElement元素下
XmlElement xmlClassElement = xmlDoc.CreateElement("class");
// 为class增加一个id属性
XmlAttribute attr = xmlDoc.CreateAttribute("id");
attr.Value = "c01";
xmlClassElement.Attributes.Append(attr);
rootElement.AppendChild(xmlClassElement);

//4.为class元素下创建一个student节点
XmlElement xmlStudentElement = xmlDoc.CreateElement("student");
XmlAttribute attrSid = xmlDoc.CreateAttribute("sid");
attrSid.Value = "s011";
xmlStudentElement.Attributes.Append(attrSid);
xmlClassElement.AppendChild(xmlStudentElement);

XmlElement xmlNameElement = xmlDoc.CreateElement("name");
xmlNameElement.InnerText = "黄林";
//5.向student节点下增加一个name节点和一个age节点
xmlStudentElement.AppendChild(xmlNameElement);

XmlElement xmlAgeElement = xmlDoc.CreateElement("Age");
xmlAgeElement.InnerText = "18";
xmlStudentElement.AppendChild(xmlAgeElement);

// 2. 将 Dom 对象写入到 xml 文件中
xmlDoc.Save("school.xml");
MessageBox.Show("OK");
#endregion
}
}
}

写入的 ​​school.xml​​ 文件如下:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<school>
<class id="c01">
<student sid="s011">
<name>黄林</name>
<Age>18</Age>
</student>
</class>
</school>

用 List 集合之循环添加

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace XML_Bzhan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
#region List 集合循环写入 xml
List<Person> list = new List<Person>() {
new Person() {Name = "黄玲", Age = 19, Email = "hl@yahoo.com"},
new Person() {Name = "徐正龙", Age = 29, Email = "xzl@yahoo.com"},
new Person() {Name = "何宏伟", Age = 39, Email = "hhw@yahoo.com"},
new Person() {Name = "杨锁", Age = 19, Email = "yangsuo@yahoo.com"},

};

// 1. 创建一个 Dom 对象
XmlDocument xDoc = new XmlDocument();
// 2. 编写文档定义
XmlDeclaration xmlDoc = xDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xDoc.AppendChild(xmlDoc);

// 3. 编写一个根节点
XmlElement xmlRoot = xDoc.CreateElement("List");
xDoc.AppendChild(xmlRoot);

// 4. 循环创建 Person 节点
for (int i = 0; i < list.Count; i++) {
// 4.1 创建一个 Person 元素
XmlElement xmlPerson = xDoc.CreateElement("Person");
XmlAttribute xmlAttrId = xDoc.CreateAttribute("id");
xmlAttrId.Value = (i + 1).ToString();
// 将属性添加到 Person 节点中
xmlPerson.Attributes.Append(xmlAttrId);
// 创建 Name
XmlElement xmlName = xDoc.CreateElement("Name");
xmlName.InnerText = list[i].Name;
xmlPerson.AppendChild(xmlName);
// 创建 Age
XmlElement xmlAge = xDoc.CreateElement("Age");
xmlAge.InnerText = list[i].Age.ToString();
xmlPerson.AppendChild(xmlAge);
// 创建 Email
XmlElement xmlEmail = xDoc.CreateElement("Email");
xmlEmail.InnerText = list[i].Email;
xmlPerson.AppendChild(xmlEmail);
// 最后把 Person 加到跟节点之下
xmlRoot.AppendChild(xmlPerson);

// 将 Dom 对象写入到 xml 文件中
xDoc.Save("list.xml");
}
MessageBox.Show("OK");
#endregion
}
}

public class Person
{
public string Name {
get;
set;
}
public int Age {
get;
set;
}
public string Email {
get;
set;
}

}
}

写入的 ​​List.xml​​文件内容:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<List>
<Person id="1">
<Name>黄玲</Name>
<Age>19</Age>
<Email>hl@yahoo.com</Email>
</Person>
<Person id="2">
<Name>徐正龙</Name>
<Age>29</Age>
<Email>xzl@yahoo.com</Email>
</Person>
<Person id="3">
<Name>何宏伟</Name>
<Age>39</Age>
<Email>hhw@yahoo.com</Email>
</Person>
<Person id="4">
<Name>杨锁</Name>
<Age>19</Age>
<Email>yangsuo@yahoo.com</Email>
</Person>
</List>

以上的方式是以编程的方式来实现的,写起来比较麻烦。

通过xmlDocument方式写入xml文件(推荐)

需添加 ​​using System.Xml.Linq;​​ 命名空间。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace XML_Bzhan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
#region 通过 XDocument 方式写入 xml 文件
List<Person> list = new List<Person>() {
new Person() {Name = "黄玲", Age = 19, Email = "hl@yahoo.com"},
new Person() {Name = "徐正龙", Age = 29, Email = "xzl@yahoo.com"},
new Person() {Name = "何宏伟", Age = 39, Email = "hhw@yahoo.com"},
new Person() {Name = "杨锁", Age = 19, Email = "yangsuo@yahoo.com"},

};

// 1.创建一个 Dom 对象
XDocument xDoc = new XDocument();// 在 using System.Xml.Linq; 命名空间下。
XDeclaration xDec = new XDeclaration("1.0", "utf-8", "no");
//xDoc.Add(xDec); // 这样写不对,会出错误。
xDoc.Declaration = xDec; // 设置 xml 文档的定义

// 2.创建根节点
XElement rootElement = new XElement("List");
xDoc.Add(rootElement);

// 3. 循环 List 集合创建 Person 节点
for (int i = 0; i < list.Count; i++) {
// 为每个 Person 对想创建一个 Person 元素
XElement xElementPerson = new XElement("Person");
xElementPerson.SetAttributeValue("id", (i + 1).ToString());

xElementPerson.SetElementValue("Name", list[i].Name);
xElementPerson.SetElementValue("Age", list[i].Age);
xElementPerson.SetElementValue("Email", list[i].Email);

rootElement.Add(xElementPerson);
}
// 4. 保存到文件
xDoc.Save("listNew.xml");
MessageBox.Show("OK");

#endregion
}

}

public class Person
{
public string Name {
get;
set;
}
public int Age {
get;
set;
}
public string Email {
get;
set;
}

}
}

输出的​​listNew.xml​​文件内容:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<List>
<Person id="1">
<Name>黄玲</Name>
<Age>19</Age>
<Email>hl@yahoo.com</Email>
</Person>
<Person id="2">
<Name>徐正龙</Name>
<Age>29</Age>
<Email>xzl@yahoo.com</Email>
</Person>
<Person id="3">
<Name>何宏伟</Name>
<Age>39</Age>
<Email>hhw@yahoo.com</Email>
</Person>
<Person id="4">
<Name>杨锁</Name>
<Age>19</Age>
<Email>yangsuo@yahoo.com</Email>
</Person>
</List>

可见,是一样的。

​xml 写入总结​

写入Xm1的两种方式:

  1. XmlDocument类,标准的Dom方式。
kmlDocument document = new XmlDocument();
document.CreateElement();
xxxxxxxx.CreateAttribute();
  1. XDocument类
XElement x = new xxx();
...Add ();

读取Xml

  1. 遍历所有节点元素,分别读取
  2. 根据节点名称,或者是id等等一些元素,快速获取某个节点。




参考:
1.​​​link-01​​ // 通过XmlDocument与XDocument方式写入Xml