LINQ学习笔记:XDocument文档与XML声明


文档与声明

XDocument

一个XDocument包装了根XElement并且允许你添加一个XDeclaration, 用于处理指令, 说明文档类型, 以及顶级的注释. XDocument是可选的, 并且能够被忽略或者省略, 这点与W3C DOM不同, 它并不是将所有的东西保持在一起的粘合剂.

XDocument提供了与XElement一样的构造函数, 是基于XContainer的, 同样也支持AddXXX, RemoveXXX以及ReplaceXXX方法. 与XElement不同的是, 一个XDocument只能接受有限的一些内容:

  • 一个单一XElement对象(根)
  • 一个单一的XDeclaration对象
  • 一个单一的XDocumentType对象(指向一个DTD)
  • 任何数量的XProcessingInstruction对象
  • 任何数量的XComment对象

所有这些, 只有根XElement是强制要求必须要有一个有效的XDocument. XDeclaration是可选的, 如果省略它, 在序列化的过程当中将会自动应用默认设置.

最简单的有效XDocument只需要有一个根元素

   1: var doc = new XDocument (
   2:             new XElement ("test", "data")
   3:           );

注意, 我们并没有包含XDeclaration对象. 但是如果你调用doc.Save保存, 你会发现文件中仍然会包含XML declaration, 因为这是默认生成的.

下面的例子产生了一个简单但正确的XHTML文件来演示XDocument能够接受的所有构造:

   1: var styleInstruction = new XProcessingInstruction (
   2:  "xml-stylesheet", "href='styles.css' type='text/css'");
   3:
   4: var docType = new XDocumentType ("html",
   5:   "-//W3C//DTD XHTML 1.0 Strict//EN",
   6:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
   7:   null);
   8:
   9: XNamespace ns = "http://www.w3.org/1999/xhtml";
  10: var root =
  11:   new XElement (ns + "html",
  12:     new XElement (ns + "head",
  13:       new XElement (ns + "title", "An XHTML page")),
  14:     new XElement (ns + "body",
  15:       new XElement (ns + "p", "This is the content"))
  16:  );
  17: var doc =
  18:   new XDocument (
  19:     new XDeclaration ("1.0", "utf-8", "no"),
  20:     new XComment ("Reference a stylesheet"),
  21:     styleInstruction,
  22:     docType,
  23:     root);
  24:
  25: doc.Save ("test.html");

最终生成的结果是:

   1: <?xml version="1.0" encoding="utf-8" standalone="no"?>
   2: <!--Reference a stylesheet-->
   3: <?xml-stylesheet xhref='styles.css' type='text/css'?>
   4: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   5:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   6: <html xmlns="http://www.w3.org/1999/xhtml">
   7:   <head>
   8:     <title>An XHTML page</title>
   9:   </head>
  10:   <body>
  11:     <p>This is the content</p>
  12:   </body>
  13: </html>

XDocument还有一个Root属性用于快速访问文档中的单一元素. 其反向的链接是有XObject的Document属性提供, 并可以应用于树中的所有对象.

   1: Console.WriteLine (doc.Root.Name.LocalName);    // html
   2: XElement bodyNode = doc.Root.Element (ns + "body");
   3: Console.WriteLine (bodyNode.Document == doc);   // True

XDeclaration不是XNode, 而且不会出现在文档的Nodes集合中, 这点与备注, 根节点, 以及processing instructions节点是不同的. 相反, 它被赋值到了一个专用的属性Declaration上. 这就是为什么下面的例子”True”只重复了4次而不是5次

   1: Console.WriteLine (doc.Root.Parent == null);    // True
   2: foreach (XNode node in doc.Nodes( ))
   3:   Console.Write
   4:     (node.Parent == null);          // TrueTrueTrueTrue

XML声明

一个标准的XML文件通常会以类似下面的声明开始:

   1: <?xml version="1.0" encoding="utf-8"standalone="yes"?>

XML声明确保XML文件能够被解析器正确解析. XElement和XDocument在处理XML声明的时候遵循以下规则:

  • 调用Save(FileName)的时候总会写入一个声明
  • 调用Save(XmlWriter)将会写入一个声明, 除非XmlWriter已经被指示不使用
  • ToString方法永远不会处理XML声明

当你构造XmlWriter的时候, 你可以通过设置XmlWriterSettings对象上的OmitXmlDeclaration和ConformanceLevel属性指示XmlWriter不产生一个声明

不论XML声明有没有被生成都不会对XDeclaration对象有任何的影响. XDeclaration的目的是用于XML序列化进程:

  • 指示什么样的encoding将会被使用
  • XML声明上的encoding和standalone属性将会被设为什么值

XDeclaration构造器接受3个参数, 分别对应于version, encoding和standalone属性. 以下的例子中, test.xml使用UTF-16编码:

   1: var doc = new XDocument (
   2:             new XDeclaration ("1.0", "utf-16", "yes"),
   3:             new XElement ("test", "data")
   4:           );
   5: doc.Save ("test.xml");

不论是设置了什么值到XML version属性上它总是会被忽略: 它总是被设置为”1.0″

encoding必须是IEIF编码的其中一种, 例如”utf-16″–它将出现在XML声明中.

待续!


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3