当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > LINQ学习笔记:XDocument文档与XML声明

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

ASP.NET 中的 LINQ学习笔记:XDocument文档与XML声明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-13   浏览: 201 ::
收藏到网摘: n/a

文档与声明

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声明中.

待续!