7 XML集成(XML Integration)
.NET 語言級集成查詢 for XML(XLinq)允許 XML 數據能夠通過使用標準查詢操作符(standard query operators)就像樹形的操作符(tree-specific operators)一樣來查詢,它能夠提供類似 XPath 的導航(XPath-like navigation)在后代(descendants),祖先(ancestors)和兄弟姐妹(siblings)的XML元素中導航。它為 XML 提供一個有效率的內存中的表現(an efficient in-memory representation),它與現有的(existing)System.Xml reader/writer 的基礎設施(infrastructure)相結合(integrates with),它比 W3C DOM 更容易使用。有三個類型在集成 XML 和查詢中(integrating XML with queries)做了大部分工作:XName,XElement 和XAttribute。
XName 提供一個有用的途徑來(an easy to use way to)處理命名空間資格的標識符(the namespace-qualified identifiers)(QNames),使用來作為元素(element)和屬性(attribute)兩個名字。XName 明顯地(transparently)控制(handle)標識符的有效率的原子操作(the efficient atomization of identifiers),允許使用符號(symbols)或純文本(plain strings)任何一個,無論一個 QName 是否需要。
XML 元素和屬性分別(respectively)使用 XElement 和 XAttribute 來表示(represented),XElement 和 XAttribute 支持常規的構造語法(normal construction syntax),允許開發者使用自然的語法(natural syntax)來寫 XML 表達式:
var e = new XElement("Person",
new XAttribute("CanCode", true),
new XElement("Name", "Loren David"),
new XElement("Age", 31));

var s = e.ToString();

這段代碼符合(corresponds to)下面的 XML:
<Person CanCode="true">
<Name>Loren David</Name>
<Age>31</Age>
</Person>

需要注意的是沒有基于 DOM 的工廠模式(DOM-based factory pattern)被需要來創建 XML 表達式,ToString 實現產生出 XML 原文(the textual XML)。XML 元素還能夠通過一個已存在的 XmlReader 或者一個字符串文本(string literal)來構建(constructed):
var e2 = XElement.Load(xmlReader);
var e1 = XElement.Parse(
@"<Person CanCode='true'>
<Name>Loren David</Name>
<Age>31</Age>
</Person>");

XElement 還支持發出 XML 數據(emitting XML),通過使用現有的 XmlWriter 類型。
XElement 與查詢操作符(query operators)相吻合(dovetails with),允許開發者針對非 XML 信息編寫查詢,通過在一個 select 語句體內(the body of a select clause)構造 XElements 來產生 XML 結果:
var query = from p in people
where p.CanCode
select new XElement("Person",
new XAttribute("Age", p.Age),
p.Name);

這個查詢返回一個 XElements 序列,為了允許 XElements 在這種查詢的結果之外構建,XElement 構造器允許元素序列(sequences of elements)可以像參數(arguments)一樣直接傳遞:
var x = new XElement("People",
from p in people
where p.CanCode
select
new XElement("Person",
new XAttribute("Age", p.Age),
p.Name));

這個 XML 表達式導致下面的 XML:
<People>
<Person Age="11">Allen Frances</Person>
<Person Age="59">Connor Morgan</Person>
</People>

上面的語句有一個直接地轉換成 Visual Basic 的方式,然而,Visual Basic 9.0 也支持 XML 文字(literals)的使用,它允許查詢表達式通過使用一個聲明式的 XML 語法(a declarative XML syntax)直接從 Visual Basic 來表達(expressed)。上面的例子可以用 Visual Basic 來構造如下所示:
Dim x = _
<People>
Select <Person Age=(p.Age) >p.Name</Person> _
From p In people _
Where p.CanCode
</People>

這個例子到目前為止展示了(so far have shown)構造了使用一個語言級集成查詢(a language-integrated query)來構造新的 XML 值。而且XElement 和 XAttribute 類型還簡單化了(simplify)信息從 XML 結構中的提取操作(the extraction of information from XML structures)。XElement 提供了訪問者方法(accessor methods)來允許查詢表達式被應用于傳統的 XPath 軸(the traditional XPath axes)。例于,如下的查詢從上面展示的 XElements 中分解出剛才的名字:
IEnumerable<string> justNames =
from e in x.Descendants("Person")
select e.Value;

//justNames = ["Allen Frances", "Connor Morgan"]

為了從 XML 中分解出結構型值(structured values),我們簡單地在 select 語句中使用一個對象初始化表達式(an object initializer expression):
IEnumerable<Person> persons =
from e in x.Descendants("Person")

select new Person
{
Name = e.Value,
Age = (int)e.Attribute("Age")
};

注意 XAttribute 和 XElement 都支持外部的 cast 操作符(explicit cast operators)來分解出文本的值(text value)為一個簡單的類型(primitive type),為了處理少見的類型(missing data),我們可以簡單地 cast 成一個可以為null 的類型(nullable type):
IEnumerable<Person> persons =
from e in x.Descendants("Person")

select new Person
{
Name = e.Value,
Age = (int?)e.Attribute("Age") ?? 21
};

在這個例子中,我們使用一個缺省數值 21,當 Age 屬性不存在時。
Visual Basic 9.0 為元素(Element),屬性(Attribute),和 XElement 的后代訪問者方法(Descendants accessor methods)提供直接的語言支持(direct language support),允許基于 XML 的數據(XML-based data)通過使用一個更加合適直接的語法(more compact, direct syntax)來訪問。我們可以使用這種功能特性(functionality)來編寫前敘的 C# 語句如下所示:
Dim persons = _

Select new Person
{
.Name = e.Name
.Age = e.@Age ?? 21
}
From e In x
Person

在 Visual Basic 里,表達式 e.Name 通過名字 Name 來找出所有的 XElements,表達式 e.@Age 通過名字 Age 來找出 XAttribute,當表達式 x...Person 通過名字 Person 來獲取 x 的后代集合容器(Descendants collection)中所有的 items。
8 總結(Summary)
.NET 語言級集成查詢(.NET Language Integrated Query)給 CLR 和語言添加了查詢能力(query capabilities),它們以此為目標。查詢技巧(facility)建立在 lambda 表達式和表達式樹基礎之上,允許斷言(predicates),映射(projections),和關鍵詞分解(key extraction)表達式用來作為不透明的可執行的代碼(opaque executable code)或作為透明的內存中的數據(transparent in-memory data)適合下游的處理和轉換(downstream processing or translation)。通過 LINQ 項目定義的標準查詢操作符(standard query operators)工作在任何基于 IEnumerable<T> 接口的信息源(IEnumerable<T>-based information source),并且與 ADO.NET(DLinq)和 System.xml(XLinq)相結合來允許關系型(relational)的和 XML 數據來獲得語言級集成查詢(language integrated query)的好處。
全文完, 錯誤難免,請批評指正,譯者Naven 2005-10-30