• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Javen-Studio 咖啡小屋

            http://javenstudio.org - C++ Java 分布式 搜索引擎
            Naven's Research Laboratory - Thinking of Life, Imagination of Future

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks

            7           XML集成(XML Integration

            .NET 語言級集成查詢 for XMLXLinq)允許 XML 數據能夠通過使用標準查詢操作符(standard query operators)就像樹形的操作符(tree-specific operators)一樣來查詢,它能夠提供類似 XPath 的導航(XPath-like navigation)在后代(descendants),祖先(ancestors)和兄弟姐妹(siblings)的XML元素中導航。它為 XML 提供一個有效率的內存中的表現(an efficient in-memory representation),它與現有的(existingSystem.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 
            = 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 = 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 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 xPerson

             

            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.NETDLinq)和 System.xmlXLinq)相結合來允許關系型(relational)的和 XML 數據來獲得語言級集成查詢(language integrated query)的好處。

             

             

             

             

            全文完, 錯誤難免,請批評指正,譯者Naven 2005-10-30

            posted on 2005-10-30 23:39 Javen-Studio 閱讀(790) 評論(0)  編輯 收藏 引用
            国产毛片久久久久久国产毛片| 97精品伊人久久大香线蕉| 国产精品欧美亚洲韩国日本久久 | 亚洲va中文字幕无码久久| 青青热久久国产久精品| 久久夜色精品国产www| 亚洲国产精品综合久久一线| 免费精品久久天干天干| 开心久久婷婷综合中文字幕| 精品无码久久久久久久动漫| 亚洲色欲久久久久综合网| 欧美久久天天综合香蕉伊| 久久久久久久久久免免费精品| 久久精品一区二区三区AV| 国产成人综合久久综合| 国产精品免费看久久久香蕉| 狠狠色丁香婷婷综合久久来| 少妇久久久久久被弄到高潮| 欧洲国产伦久久久久久久| 伊人久久大香线蕉综合网站| 色偷偷88欧美精品久久久| 人妻无码αv中文字幕久久琪琪布| 人妻无码精品久久亚瑟影视| 国产亚洲精品久久久久秋霞| 国产视频久久| 亚洲午夜精品久久久久久浪潮| 久久人人爽人人人人片av| 欧洲精品久久久av无码电影| 久久久久亚洲AV成人网人人网站| 久久久久无码中| 久久婷婷五月综合国产尤物app| 99久久久精品免费观看国产| 国产精品中文久久久久久久| 狠狠色婷婷久久综合频道日韩| 久久精品国产亚洲77777| 久久久这里有精品| 国产精品成人无码久久久久久| 亚洲一级Av无码毛片久久精品| 97精品伊人久久大香线蕉app | 大美女久久久久久j久久| 亚洲国产日韩综合久久精品|