程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> LINQ學習筆記:XDocument文檔與XML聲明

LINQ學習筆記:XDocument文檔與XML聲明

編輯:關於ASP.NET

文檔與聲明

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

待續!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved