Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XNode.ReadFrom Method

Creates an XNode from an XmlReader.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public static XNode ReadFrom(
	XmlReader reader
)

Parameters

reader
Type: System.Xml.XmlReader
An XmlReader positioned at the node to read into this XNode.

Return Value

Type: System.Xml.Linq.XNode
An XNode that contains the node and its descendant nodes that were read from the reader. The runtime type of the node is determined by the node type (NodeType) of the first node encountered in the reader.

ExceptionCondition
InvalidOperationException

The XmlReader is not positioned on a recognized node type.

XmlException

The underlying XmlReader throws an exception.

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. 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 does not catch all exceptions thrown by the reader; the unhandled exceptions bubble up to the code that called ReadFrom. In particular, your code should be prepared to handle XmlException.

For an example of how to stream a more complex document, see How to: Stream XML Fragments with Access to Header Information in the .NET Framework documentation.

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.

For an example of using LINQ to XML to transform extremely large XML documents while maintaining a small memory footprint, see How to: Perform Streaming Transform of Large XML Documents in the .NET Framework documentation.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft