XNode.ReadFrom(XmlReader) メソッド

定義

XmlReaderからXNodeを作成します。

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

このXNodeに読み込むノードに配置されたXmlReader

返品

リーダーから読み取られたノードとその子孫ノードを含む 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 ドキュメントのストリーミング変換を実行する方法」を参照してください。

適用対象

こちらもご覧ください