Export (0) Print
Expand All

XElement.Load Method (XmlReader, LoadOptions)

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)

'Declaration
Public Shared Function Load ( _
	reader As XmlReader, _
	options As LoadOptions _
) As XElement

Parameters

reader
Type: System.Xml.XmlReader
A XmlReader that will be read for the content of the XElement.
options
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.

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.

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


Dim output As New StringBuilder
Dim markup As String = _
    "<Root>" & Environment.NewLine & _
    "    <Child>" & Environment.NewLine & _
    "        <GrandChild/>" & Environment.NewLine & _
    "    </Child>" & Environment.NewLine & _
    "</Root>"

' Create a reader and move to the content.
Using nodeReader As XmlReader = XmlReader.Create(New StringReader(markup))

    ' the reader must be in the Interactive state in order to
    ' Create a LINQ to XML tree from it.
    nodeReader.MoveToContent()

    Dim xRoot As XElement = XElement.Load(nodeReader, LoadOptions.SetLineInfo)
    output.Append(String.Format("{0}{1}{2}", _
        "Element Name".PadRight(20), _
        "Line".PadRight(5), _
        "Position"))
    output.Append(Environment.NewLine)
    output.Append(String.Format("{0}{1}{2}", _
        "------------".PadRight(20), _
        "----".PadRight(5), _
        "--------"))
    output.Append(Environment.NewLine)
    For Each e As XElement In xRoot.DescendantsAndSelf()
        output.Append(String.Format("{0}{1}{2}", _
            ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString).PadRight(20), _
            (DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
            (DirectCast(e, IXmlLineInfo)).LinePosition))
        output.Append(Environment.NewLine)
    Next
End Using

OutputTextBlock.Text = output.ToString()


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