XNode.ReadFrom(XmlReader) 方法

定義

XNode 建立 XmlReaderCreates an XNode from an XmlReader.

public:
 static System::Xml::Linq::XNode ^ ReadFrom(System::Xml::XmlReader ^ reader);
public static System.Xml.Linq.XNode ReadFrom (System.Xml.XmlReader reader);
static member ReadFrom : System.Xml.XmlReader -> System.Xml.Linq.XNode
Public Shared Function ReadFrom (reader As XmlReader) As XNode

參數

reader
XmlReader

XmlReader 位於節點,以讀入這個 XNodeAn XmlReader positioned at the node to read into this XNode.

傳回

XNode,包含從讀取器讀取的節點及其子代節點。An XNode that contains the node and its descendant nodes that were read from the reader. 節點的執行階段型別是由讀取器中遇到的第一個節點的節點型別 (NodeType) 決定的。The runtime type of the node is determined by the node type (NodeType) of the first node encountered in the reader.

例外狀況

XmlReader 未位於可識別的節點型別上。The XmlReader is not positioned on a recognized node type.

基礎 XmlReader 會擲回例外狀況。The underlying XmlReader throws an exception.

範例

這個範例會使用下列名為Source.XML 的 xml 檔案:This example uses the following XML file, named Source.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Child Key="01">
    <GrandChild>aaa</GrandChild>
  </Child>
  <Child Key="02">
    <GrandChild>bbb</GrandChild>
  </Child>
  <Child Key="03">
    <GrandChild>ccc</GrandChild>
  </Child>
</Root>

下列範例會建立使用ReadFrom的自訂座標軸方法,然後使用 LINQ 查詢來查詢自訂軸:The following example creates a custom axis method that uses ReadFrom and then queries the custom axis by using a LINQ query:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static IEnumerable<XElement> StreamRootChildDoc(string uri)
    {
        using (XmlReader reader = XmlReader.Create(uri))
        {
            reader.MoveToContent();
            
            // Parse the file and return each of the nodes.
            while (!reader.EOF)
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "Child")
                {
                    XElement el = XElement.ReadFrom(reader) as XElement;
                    if (el != null)
                        yield return el;
                }
                else
                    reader.Read();
            }
        }
    }

    static void Main(string[] args)
    {
        IEnumerable<string> grandChildData =
            from el in StreamRootChildDoc("Source.xml")
            where (int)el.Attribute("Key") > 1
            select (string)el.Element("GrandChild");

        foreach (string str in grandChildData)
            Console.WriteLine(str);
    }
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq

Module Program
    Iterator Function StreamRootChildDoc(ByVal uri As String) As IEnumerable(Of XElement)

        Using reader As XmlReader = XmlReader.Create(uri)
            reader.MoveToContent()

            ' Parse the file and return each of the nodes.
            While Not reader.EOF

                If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "Child" Then
                    Dim el As XElement = TryCast(XElement.ReadFrom(reader), XElement)
                    If el IsNot Nothing Then Yield el
                Else
                    reader.Read()
                End If
            End While
        End Using
    End Function

    Sub Main(args As String())

        Dim grandChildData As IEnumerable(Of String) =
            From el In StreamRootChildDoc("Source.xml")
            Where CInt(el.Attribute("Key")) > 1
            Select CStr(el.Element("GrandChild"))

        For Each str As String In grandChildData
            Console.WriteLine(str)
        Next

    End Sub

End Module

這個範例會產生下列輸出:This example produces the following output:

bbb  
ccc  

備註

您可以使用這個方法來撰寫方法,以傳回節點的集合,並在從讀取器讀取節點時產生每個節點。You can use this method to write a method that returns a collection of nodes, yielding each node as the node is read from the reader. 這個方法可讓您處理具有極小記憶體使用量的任意大型 XML 檔案。This method enables you to process arbitrarily large XML files with a very small memory footprint.

您傳遞給這個方法的讀取器可能會擲回例外狀況。The reader that you pass to this method might throw exceptions. ReadFrom不會攔截讀取器擲回的所有例外狀況;未處理的例外狀況會反升至呼叫ReadFrom的程式碼。ReadFrom does not catch all exceptions thrown by the reader; the unhandled exceptions bubble up to the code that called ReadFrom. 特別是,您的程式碼應該已準備XmlException好處理。In particular, your code should be prepared to handle XmlException.

如需如何串流更複雜檔的範例,請參閱如何:具有標頭資訊存取權的資料流程 XML 片段。For an example of how to stream a more complex document, see How to: Stream XML Fragments with Access to Header Information.

特定的標準查詢運算子 (例如,OrderBy) 會反覆查看其來源、收集所有資料、排序這些資料,最後產生順序中的第一個項目。Certain standard query operators, such as OrderBy, iterate their source, collect all of the data, sort it, and then finally yield the first item in the sequence. 如果您在產生第一個專案前使用具體化其來源的查詢運算子,您將不會保留小型的記憶體使用量。If you use a query operator that materializes its source before yielding the first item, you will not retain a small memory footprint.

如需使用LINQ to XMLLINQ to XML來轉換非常大的 XML 檔,同時維持小型記憶體使用量的範例, 請參閱如何:執行大型 XML 檔的資料流程轉換。For an example of using LINQ to XMLLINQ to XML to transform extremely large XML documents while maintaining a small memory footprint, see How to: Perform Streaming Transform of Large XML Documents.

適用於

另請參閱