XDocument.Load Method (XmlReader, LoadOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Creates a new XDocument from an XmlReader, optionally setting the base URI, and retaining line information.
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
Parameters
- reader
- Type: System.Xml.XmlReader
A XmlReader that will be read for the content of the XDocument.
- options
- Type: System.Xml.Linq.LoadOptions
A LoadOptions that specifies whether to load base URI and line information.
Return Value
Type: System.Xml.Linq.XDocumentAn XDocument that contains the XML that was read from the specified XmlReader.
Use Parse to create an XDocument 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.
StringBuilder output = new StringBuilder(); string markup = @"<Root> <Child> <GrandChild/> </Child> </Root>"; // Create a reader and move to the content. using (XmlReader nodeReader = 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(); XDocument xRoot = XDocument.Load(nodeReader, LoadOptions.SetLineInfo); output.Append("Element Name".PadRight(20) + "Line".PadRight(5) + "Position" + Environment.NewLine); output.Append("------------".PadRight(20) + "----".PadRight(5) + "--------" + Environment.NewLine); foreach (XElement e in xRoot.Elements("Root").DescendantsAndSelf()) output.Append(("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20) + ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5) + ((IXmlLineInfo)e).LinePosition + Environment.NewLine); } OutputTextBlock.Text = output.ToString();