Export (0) Print
Expand All

XElement.Load Method (String, LoadOptions)

Loads an XElement from a file, 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 ( _
	uri As String, _
	options As LoadOptions _
) As XElement

Parameters

uri
Type: System.String

A URI string referencing the file to load into an 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 contents of the specified file.

If the source XML is indented, setting the PreserveWhitespace flag in options causes the reader to read all white space in the source XML. Nodes of type XText are created for both significant and insignificant white space.

If the source XML is indented, not setting the PreserveWhitespace flag in options causes the reader to ignore all of the insignificant white space in the source XML. The XML tree is created without any text nodes for insignificant white space.

If the source XML is not indented, setting the PreserveWhitespace flag in options has no effect. Significant white space is still preserved, and there are no spans of insignificant white space that could cause the creation of more white space text nodes.

For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

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

There is a performance penalty if you set the SetBaseUri and the SetLineInfo flags.

The base URI and the line information are accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the base URI and 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 an XElement from a file in two different ways: preserving white space, and not preserving white space. It then uses a query to determine the number of white space nodes in the resulting XML tree.

Dim xmlTree1 As XElement = XElement.Parse("<Root> <Child>  </Child> </Root>", LoadOptions.PreserveWhitespace)
xmlTree1.Save("Tree.xml")
Console.WriteLine(xmlTree1)

Dim whiteSpaceNodes As Integer
Dim xmlTree2 As XElement = XElement.Load("Tree.xml", LoadOptions.None)
whiteSpaceNodes = xmlTree2 _
                  .DescendantNodesAndSelf() _
                  .OfType(Of XText)() _
                  .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
                  .Count()
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes)

Dim xmlTree3 As XElement = XElement.Load("Tree.xml", LoadOptions.PreserveWhitespace)
whiteSpaceNodes = xmlTree3 _
                  .DescendantNodesAndSelf() _
                  .OfType(Of XText)() _
                  .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
                  .Count()
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes)

This example produces the following output:

<Root> <Child>  </Child> </Root>
Count of white space nodes (not preserving whitespace): 0
Count of white space nodes (preserving whitespace): 3

The following example loads the base URI and line information as it loads the file. It then prints the base URI and the line information.

This example uses the following resource file: Sample XML File: Typical Purchase Order (LINQ to XML).

        Dim po As XElement = XElement.Load("PurchaseOrder.xml", LoadOptions.SetBaseUri Or LoadOptions.SetLineInfo)
        Dim splitUri() As String = po.BaseUri.Split("/")
        Console.WriteLine("BaseUri: {0}", splitUri(splitUri.Length - 1))
        Console.WriteLine()
        Console.WriteLine("{0}{1}{2}", _
            "Element Name".PadRight(20), _
            "Line".PadRight(5), _
            "Position")
        Console.WriteLine("{0}{1}{2}", _
            "------------".PadRight(20), _
            "----".PadRight(5), _
            "--------")
        For Each e As XElement In po.DescendantsAndSelf()
            Console.WriteLine("{0}{1}{2}", _
                ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString()).PadRight(20), _
                (DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
                (DirectCast(e, IXmlLineInfo)).LinePosition)
        Next

This example produces the following output:

BaseUri: PurchaseOrder.xml

Element Name        Line Position
------------        ---- --------
PurchaseOrder       2    2
  Address           3    4
    Name            4    6
    Street          5    6
    City            6    6
    State           7    6
    Zip             8    6
    Country         9    6
  Address           11   4
    Name            12   6
    Street          13   6
    City            14   6
    State           15   6
    Zip             16   6
    Country         17   6
  DeliveryNotes     19   4
  Items             20   4
    Item            21   6
      ProductName   22   8
      Quantity      23   8
      USPrice       24   8
      Comment       25   8
    Item            27   6
      ProductName   28   8
      Quantity      29   8
      USPrice       30   8
      ShipDate      31   8

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft