语言

XNode.ReadFrom(XmlReader) 方法

定义

从 . 创建一个 XNodeXmlReader。

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 文档的流式转换。

适用于

另请参阅