XNode.ReadFrom(XmlReader) 方法

定義

XNode 建立 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 位於節點,以讀入這個 XNode

傳回

XNode,包含從讀取器讀取的節點及其子代節點。 節點的執行階段型別是由讀取器中遇到的第一個節點的節點型別 (NodeType) 決定的。

例外狀況

XmlReader 未位於可識別的節點型別上。

基礎 XmlReader 會擲回例外狀況。

範例

此範例會使用名為 Source.xml的下列 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 查詢來查詢自訂座標軸:

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

這個範例會產生下列輸出:

bbb  
ccc  

備註

您可以使用此方法來撰寫傳回節點集合的方法,並產生每個節點,因為讀取器讀取節點。 這個方法可讓您處理具有非常小記憶體使用量的任意大型 XML 檔案。

您傳遞至這個方法的讀取器可能會擲回例外狀況。 ReadFrom 不會攔截讀取器擲回的所有例外狀況;未處理的例外狀況會反升至呼叫 ReadFrom 的程式碼。 特別是,您的程式碼應該準備好處理 XmlException

如需如何串流更複雜的檔範例,請參閱 如何串流具有標頭資訊存取權的 XML 片段

特定的標準查詢運算子 (例如,OrderBy) 會反覆查看其來源、收集所有資料、排序這些資料,最後產生順序中的第一個項目。 如果您在產生第一個專案之前使用具體化其來源的查詢運算子,則不會保留少量的記憶體使用量。

如需使用LINQ to XML轉換極大型 XML 檔的範例,同時維持少量的記憶體使用量,請參閱如何執行大型 XML 檔的串流轉換

適用於

另請參閱