XElement.Load Method (XmlReader, LoadOptions)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Loads an XElement from an XmlReader, optionally preserving white space, setting the base URI, and retaining line information.

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

static member Load : 
        reader:XmlReader *
        options:LoadOptions -> XElement


Type: System.Xml.XmlReader

A XmlReader that will be read for the content of the XElement.

Type: System.Xml.Linq.LoadOptions

A LoadOptions that specifies white space behavior, and whether to load base URI and line information.

Return Value

Type: System.Xml.Linq.XElement

An XElement that contains the XML that was read from the specified XmlReader.

By creating an XmlNodeReader from a DOM document, and then using the XmlNodeReader to create an XElement, this method can be used to create a copy of a DOM document in a LINQ to XML tree.

Use Parse to create an XElement from a string that contains XML.

Setting PreserveWhitespace is not valid when loading from a XmlReader. The XmlReader will be configured to either read whitespace or not. The LINQ to XML tree will be populated with the whitespace nodes that the reader surfaces. This will be the behavior regardless of whether PreserveWhitespace is set or not.

The XmlReader may have a valid base URI or not. If you set SetBaseUri, the base URI will be set in the XML tree from the base URI that is reported by the XmlReader.

The XmlReader may have a valid line information or not. If you set SetLineInfo, the line information will be set in the XML tree from the line information that is reported by the XmlReader.

There is a performance penalty if you set the SetLineInfo flag.

The line information is accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the line information may become meaningless.

LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.

The following example loads the line information that it loads from the XmlReader. It then prints the line information.

No code example is currently available or this language may not be supported.

This example produces the following output:

                Element Name        Line Position
------------        ---- --------
Root                1    2
  Child             2    6
    GrandChild      3    10

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top