加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML

发布时间:2020-12-16 00:07:35 所属栏目:百科 来源:网络整理
导读:XML基础 ?xml version="1.0" encoding="utf-8"? Books Book BookName date = "a" author = "x" b / BookName / Book / Books XML(Extensible Makeup Language)可扩展标记语言,用来存储数据,规范了数据的存储结构(为每一个数据界定了范围),以便更方便的

XML基础

<?xml version="1.0" encoding="utf-8"?>

<Books>

    <Book>

        <BookName date="a" author="x">b</BookName>

    </Book>

</Books>

XML(Extensible Makeup Language)可扩展标记语言,用来存储数据,规范了数据的存储结构(为每一个数据界定了范围),以便更方便的操作数据。
可用来做配置文件(使用软件时,为用户加载所需环境的设置,比如窗口的大小及位置)

文档声明

<?xml version="1.0" encoding="utf-8"?>

每个XML文档都要包含文档声明,version当前只有两个值:1.0和1.1,但vs不支持1.1。1.1并没有给Windows平台上使用的XML做什么改进,而且World Wide Web Consortium(www.w3c.org)建议尽可能使用第一版。
encoding的值表示用于读取文档的字符集

根元素

每个文档都要有且只有一个根元素

<Books>

</Books>

元素

XML元素包含一个开始标记,元素中的数据和结束标记<a>qwe</a>
但如果里边没有嵌套的元素,可以写成<a />
元素的名称区分大小写<book><Book>是不同的
元素的名称自己定义
元素可以包含其他元素,但元素不允许重叠,在父元素的结束标记之前必须关闭所有子元素

特性

<BookName date="qeqe" author="wewr">qwe</BookName>

除了在元素体内存储数据之外,也可以在特性内存储数据,以键值对的形式存,特性值必须包含在单引号或双引号内。存储多个特性用空格分隔。

元素和特性并没有太大的区别,如果以后需要为数据添加更多信息,最好使用元素,特性更便于保存对文档的每一位用户而言无关紧要的信息。可以根据自己的爱好选择。

XML文档的各个组成部分成为节点——元素,元素内的数据,特性,文档声明都是XML文档的节点。

DOM操作

XML文档对象模型(Document Object Model,DOM)
将XML作为一个对象
节点都是对象
命名空间System.Xml
创建文档 创建节点 追加结点 保存文档

using System.Xml;

namespace 集合
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xDocument = new XmlDocument();//在内存中创建一个文档对象

            XmlDeclaration xDeclaration = xDocument.CreateXmlDeclaration("1.0","utf-8",null);//创建声明
            xDocument.AppendChild(xDeclaration);//把声明写到文档中

            XmlElement xRoot = xDocument.CreateElement("Persons");//创建根节点xRoot
            xDocument.AppendChild(xRoot); //把根节点xRoot添加到文档xDocument

            XmlElement xPerson1 = xDocument.CreateElement("Person");//创建子节点xPerson1
            xPerson1.SetAttribute("id","1");//给子节点xPerson添加特性
            xRoot.AppendChild(xPerson1);//把子节点Person添加到根节点xRoot

            XmlElement xName1 = xDocument.CreateElement("Name");////创建子节点xName1
            xName1.InnerText = "Olive";//给子节点xPName添加数据Olive
            xPerson1.AppendChild(xName1);//给xPerson1添加节点xName1

            XmlElement xAge1 = xDocument.CreateElement("Age");
            xAge1.InnerText = "22";
            xPerson1.AppendChild(xAge1);

            XmlElement xPerson2 = xDocument.CreateElement("Person");
            xPerson2.SetAttribute("id","2");
            xRoot.AppendChild(xPerson2);

            XmlElement xName2 = xDocument.CreateElement("Name");
            xName2.InnerText = "Jenny";
            xPerson2.AppendChild(xName2);

            XmlElement xAge2 = xDocument.CreateElement("Age");
            xAge2.InnerText = "23";
            xPerson2.AppendChild(xAge2);

            XmlElement xPerson3 = xDocument.CreateElement("Person");
            xPerson3.SetAttribute("id","3");
            xRoot.AppendChild(xPerson3);

            XmlElement xName3 = xDocument.CreateElement("Name");
            xName3.InnerText = "Danny";
            xPerson3.AppendChild(xName3);

            XmlElement xAge3 = xDocument.CreateElement("Age");
            xAge3.InnerText = "24";
            xPerson3.AppendChild(xAge3);


            xDocument.Save("Persons.xml");//保存文件到硬盘
        }

    }
}

/* <?xml version="1.0" encoding="utf-8"?> <Persons> <Person id="1"> <Name>Olive</Name> <Age>22</Age> </Person> <Person id="2"> <Name>Jenny</Name> <Age>23</Age> </Person> <Person id="3"> <Name>Danny</Name> <Age>24</Age> </Person> </Persons> **/

Linq to XML

命名空间:System.Xml.Linq
常用类:XDocument XElement XAttribute
传统方法 创建文档 创建节点 追加结点 保存文档
Linq方法 使用构造函数的重载,链式创建文档

using System.Xml.Linq;

namespace 集合
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xDocument = new XDocument(
                new XElement("Persons",new XElement("Person",new XAttribute("id","1"),new XElement("Name","Olive"),new XElement("Age","22")),"2"),"Jenny"),"23")),"3"),"Denny"),"24"))
                       ));
            xDocument.Save("Person.xml");

        }

    }
} 

/*
<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person id="1">
    <Name>Olive</Name>
    <Age>22</Age>
  </Person>
  <Person id="2">
    <Name>Jenny</Name>
    <Age>23</Age>
  </Person>
  <Person id="3">
    <Name>Danny</Name>
    <Age>24</Age>
  </Person>
</Persons>
**/

XElement.Add() XElement.AddFirst() XElement.Remove()方法或者XElement.RemoveAll()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读