XElement::Load Method (String^, LoadOptions)
Loads an XElement from a file, optionally preserving white space, setting the base URI, and retaining line information.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
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 XML3 and Preserving White Space While Serializing1.
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.
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)3.
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
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1