XDocument.Load 方法

定义

从 URI 所指定的文件、XDocumentTextReader 创建新 XmlReader

重载

Load(Stream)

使用指定的流创建一个新的 XDocument 实例。

Load(TextReader)

XDocument 创建新的 TextReader

Load(String)

从文件创建新 XDocument

Load(XmlReader)

XDocument 创建新 XmlReader

Load(Stream, LoadOptions)

使用指定流创建新的 XDocument 实例,也可以选择保留空白,设置基 URI 和保留行信息。

Load(TextReader, LoadOptions)

XDocument 创建新 TextReader,还可以选择保留空白和行信息以及设置基 URI。

Load(String, LoadOptions)

从文件创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。

Load(XmlReader, LoadOptions)

XDocument 加载 XmlReader,还可以选择设置基 URI 和保留行信息。

注解

使用此方法的重载之一 XDocument ,可以从文件、文件 TextReader或文件 XmlReader加载。

若要从包含 XML 的字符串创建一个 XDocument ,请使用 Parse

Load(Stream)

使用指定的流创建一个新的 XDocument 实例。

public:
 static System::Xml::Linq::XDocument ^ Load(System::IO::Stream ^ stream);
public static System.Xml.Linq.XDocument Load (System.IO.Stream stream);
static member Load : System.IO.Stream -> System.Xml.Linq.XDocument
Public Shared Function Load (stream As Stream) As XDocument

参数

stream
Stream

包含 XML 数据的流。

返回

XDocument

一个可读取流中所包含数据的 XDocument 对象。

注解

如果要控制加载选项,请使用 Load 采用 LoadOptions 参数的重载。

LINQ to XML 的加载功能基于 XmlReader。 因此,你可能会捕获重载方法和XmlReader读取和分析文档的方法引发XmlReader.Create的任何异常。

如果必须修改 XmlReaderSettings,请执行以下步骤:

  1. XmlReader通过调用作为参数的Create重载XmlReaderSettings之一来创建一个。

  2. XmlReader它用作XmlReader参数的LoadXDocument重载之一传递。

适用于

Load(TextReader)

XDocument 创建新的 TextReader

public:
 static System::Xml::Linq::XDocument ^ Load(System::IO::TextReader ^ textReader);
public static System.Xml.Linq.XDocument Load (System.IO.TextReader textReader);
static member Load : System.IO.TextReader -> System.Xml.Linq.XDocument
Public Shared Function Load (textReader As TextReader) As XDocument

参数

textReader
TextReader

一个 TextReader,其中包含 XDocument 的内容。

返回

XDocument

一个 XDocument,其中包含指定 TextReader 的内容。

示例

The following example creates a document from a StringReader.

TextReader tr = new StringReader("<Root>Content</Root>");  
XDocument doc = XDocument.Load(tr);  
Console.WriteLine(doc);  
Dim tr As TextReader = New StringReader("<Root>Content</Root>")  
Dim doc As XDocument = XDocument.Load(tr)  
Console.WriteLine(doc)  

该示例产生下面的输出:

<Root>Content</Root>  

注解

LINQ to XML 的加载功能基于 XmlReader。 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

另请参阅

适用于

Load(String)

从文件创建新 XDocument

public:
 static System::Xml::Linq::XDocument ^ Load(System::String ^ uri);
public static System.Xml.Linq.XDocument Load (string uri);
static member Load : string -> System.Xml.Linq.XDocument
Public Shared Function Load (uri As String) As XDocument

参数

uri
String

一个 URI 字符串,它引用要加载到新 XDocument 中的文件。

返回

XDocument

一个包含指定文件的内容的 XDocument

示例

下面的示例演示如何从文件加载。XDocument

此示例使用下面的 XML 文档:

示例 XML 文件:典型采购订单 (LINQ to XML)

XDocument doc = XDocument.Load("PurchaseOrder.xml");  
Console.WriteLine(doc);  
Dim doc As XDocument = XDocument.Load("PurchaseOrder.xml")  
Console.WriteLine(doc)  

该示例产生下面的输出:

<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">  
  <Address Type="Shipping">  
    <Name>Ellen Adams</Name>  
    <Street>123 Maple Street</Street>  
    <City>Mill Valley</City>  
    <State>CA</State>  
    <Zip>10999</Zip>  
    <Country>USA</Country>  
  </Address>  
  <Address Type="Billing">  
    <Name>Tai Yee</Name>  
    <Street>8 Oak Avenue</Street>  
    <City>Old Town</City>  
    <State>PA</State>  
    <Zip>95819</Zip>  
    <Country>USA</Country>  
  </Address>  
  <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>  
  <Items>  
    <Item PartNumber="872-AA">  
      <ProductName>Lawnmower</ProductName>  
      <Quantity>1</Quantity>  
      <USPrice>148.95</USPrice>  
      <Comment>Confirm this is electric</Comment>  
    </Item>  
    <Item PartNumber="926-AA">  
      <ProductName>Baby Monitor</ProductName>  
      <Quantity>2</Quantity>  
      <USPrice>39.98</USPrice>  
      <ShipDate>1999-05-21</ShipDate>  
    </Item>  
  </Items>  
</PurchaseOrder>  

注解

此方法使用基础 XmlReader 将 XML 读入 XML 树。

用于 Parse 从包含 XML 的字符串创建一个 XDocument

LINQ to XML 的加载功能基于 XmlReader. 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

另请参阅

适用于

Load(XmlReader)

XDocument 创建新 XmlReader

public:
 static System::Xml::Linq::XDocument ^ Load(System::Xml::XmlReader ^ reader);
public static System.Xml.Linq.XDocument Load (System.Xml.XmlReader reader);
static member Load : System.Xml.XmlReader -> System.Xml.Linq.XDocument
Public Shared Function Load (reader As XmlReader) As XDocument

参数

reader
XmlReader

一个 XmlReader,其中包含 XDocument 的内容。

返回

XDocument

一个 XDocument,其中包含指定 XmlReader 的内容。

示例

The following example creates a DOM document, creates an XmlNodeReader from the DOM document, creates an XDocument using the XmlNodeReader.

// Create a DOM document with some content.  
XmlDocument doc = new XmlDocument();  
XmlElement child = doc.CreateElement("Child");  
child.InnerText = "child contents";  
XmlElement root = doc.CreateElement("Root");  
root.AppendChild(child);  
doc.AppendChild(root);  

// create a reader and move to the content  
using (XmlNodeReader nodeReader = new XmlNodeReader(doc)) {  
    // 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);  
    Console.WriteLine(xRoot);  
}  
' Create a DOM document with some content.  
Dim doc As XmlDocument = New XmlDocument()  
Dim child As XmlElement = doc.CreateElement("Child")  
child.InnerText = "child contents"  
Dim root As XmlElement = doc.CreateElement("Root")  
root.AppendChild(child)  
doc.AppendChild(root)  

' create a reader and move to the content  
Using nodeReader = New XmlNodeReader(doc)  
    ' the reader must be in the Interactive state in order to  
    ' create a LINQ to XML tree from it.  
    nodeReader.MoveToContent()  

    Dim xRoot As XDocument = XDocument.Load(nodeReader)  
    Console.WriteLine(xRoot)  
End Using  

该示例产生下面的输出:

<Root>  
  <Child>child contents</Child>  
</Root>  

注解

此方法的一个可能用途是在 LINQ to XML 树中创建 DOM 文档的副本。 为此,请从 DOM 文档创建一个 XmlNodeReader ,然后使用 XmlNodeReader 该文档创建一个 XDocument

LINQ to XML 的加载功能基于 XmlReader. 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

另请参阅

适用于

Load(Stream, LoadOptions)

使用指定流创建新的 XDocument 实例,也可以选择保留空白,设置基 URI 和保留行信息。

public:
 static System::Xml::Linq::XDocument ^ Load(System::IO::Stream ^ stream, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XDocument Load (System.IO.Stream stream, System.Xml.Linq.LoadOptions options);
static member Load : System.IO.Stream * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XDocument
Public Shared Function Load (stream As Stream, options As LoadOptions) As XDocument

参数

stream
Stream

包含 XML 数据的流。

options
LoadOptions

一个 LoadOptions,指定是否加载基 URI 和行信息。

返回

XDocument

一个可读取流中所包含数据的 XDocument 对象。

注解

LINQ to XML 的加载功能基于 XmlReader。 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

若要修改 XmlReaderSettings,请执行以下步骤:

  1. XmlReader通过调用作为参数的Create重载XmlReaderSettings之一来创建。

  2. XmlReader它作为参数传递给其中XmlReaderLoad个重载XDocument

适用于

Load(TextReader, LoadOptions)

XDocument 创建新 TextReader,还可以选择保留空白和行信息以及设置基 URI。

public:
 static System::Xml::Linq::XDocument ^ Load(System::IO::TextReader ^ textReader, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XDocument Load (System.IO.TextReader textReader, System.Xml.Linq.LoadOptions options);
static member Load : System.IO.TextReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XDocument
Public Shared Function Load (textReader As TextReader, options As LoadOptions) As XDocument

参数

textReader
TextReader

一个 TextReader,其中包含 XDocument 的内容。

options
LoadOptions

一个 LoadOptions,指定空白行为以及是否加载基 URI 和行信息。

返回

XDocument

一个 XDocument,其中包含从指定的 TextReader 读取的 XML。

示例

The following example creates a document from a StringReader.

TextReader sr;  
int whiteSpaceNodes;  

sr = new StringReader("<Root> <Child> </Child> </Root>");  
XDocument xmlTree1 = XDocument.Load(sr, LoadOptions.None);  
sr.Close();  
whiteSpaceNodes = xmlTree1  
    .Element("Root")  
    .DescendantNodesAndSelf()  
    .OfType<XText>()  
    .Where(tNode => tNode.ToString().Trim().Length == 0)  
    .Count();  
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes);  

sr = new StringReader("<Root> <Child> </Child> </Root>");  
XDocument xmlTree2 = XDocument.Load(sr, LoadOptions.PreserveWhitespace);  
sr.Close();  
whiteSpaceNodes = xmlTree2  
    .Element("Root")  
    .DescendantNodesAndSelf()  
    .OfType<XText>()  
    .Where(tNode => tNode.ToString().Trim().Length == 0)  
    .Count();  
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes);  
Dim sr As TextReader  
Dim whiteSpaceNodes As Integer  

sr = New StringReader("<Root> <Child> </Child> </Root>")  
Dim xmlTree1 As XDocument = XDocument.Load(sr, LoadOptions.None)  
sr.Close()  
whiteSpaceNodes = xmlTree1 _  
              .Element("Root") _  
              .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)  

sr = New StringReader("<Root> <Child> </Child> </Root>")  
Dim xmlTree2 As XDocument = XDocument.Load(sr, LoadOptions.PreserveWhitespace)  
sr.Close()  
whiteSpaceNodes = xmlTree2 _  
              .Element("Root") _  
              .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)  

该示例产生下面的输出:

Count of white space nodes (not preserving whitespace): 0  
Count of white space nodes (preserving whitespace): 3  

注解

如果源 XML 缩进,则 PreserveWhitespace 设置标志 options 会导致读取器读取源 XML 中的所有空白。 为重要且微不足道的空白创建类型 XText 节点。

如果源 XML 缩进,则不设置 PreserveWhitespace 标志 options 会导致读取器忽略源 XML 中所有无关紧要的空白。 创建 XML 树时没有任何文本节点用于无关紧要的空白。

如果源 XML 未缩进,则 PreserveWhitespace 设置 options 标志不起作用。 仍然保留大量空白,并且没有无关紧要的空白空间,这可能会导致创建更多的空白文本节点。

有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白

用于 Parse 从包含 XML 的字符串创建一个 XElement

从中TextReader加载时设置SetBaseUri无效。

如果设置 SetLineInfo 标志,性能会降低。

加载 XML 文档后,行信息会立即准确。 如果在加载文档后修改 XML 树,则行信息可能毫无意义。

LINQ to XML 的加载功能基于 XmlReader. 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

另请参阅

适用于

Load(String, LoadOptions)

从文件创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。

public:
 static System::Xml::Linq::XDocument ^ Load(System::String ^ uri, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XDocument Load (string uri, System.Xml.Linq.LoadOptions options);
static member Load : string * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XDocument
Public Shared Function Load (uri As String, options As LoadOptions) As XDocument

参数

uri
String

一个 URI 字符串,它引用要加载到新 XDocument 中的文件。

options
LoadOptions

一个 LoadOptions,指定空白行为以及是否加载基 URI 和行信息。

返回

XDocument

一个包含指定文件的内容的 XDocument

示例

下面的示例演示如何从文件加载。XDocument

此示例使用下面的 XML 文档:

示例 XML 文件:典型采购订单 (LINQ to XML)

XDocument doc1 = XDocument.Load("PurchaseOrder.xml", LoadOptions.None);  
Console.WriteLine("nodes if not preserving whitespace: {0}", doc1.DescendantNodes().Count());  

XDocument doc2 = XDocument.Load("PurchaseOrder.xml", LoadOptions.PreserveWhitespace);  
Console.WriteLine("nodes if preserving whitespace: {0}", doc2.DescendantNodes().Count());  
Dim doc1 As XDocument = XDocument.Load("PurchaseOrder.xml", LoadOptions.None)  
Console.WriteLine("nodes if not preserving whitespace: {0}", doc1.DescendantNodes().Count())  

Dim doc2 As XDocument = XDocument.Load("PurchaseOrder.xml", LoadOptions.PreserveWhitespace)  
Console.WriteLine("nodes if preserving whitespace: {0}", doc2.DescendantNodes().Count())  

该示例产生下面的输出:

nodes if not preserving whitespace: 48  
nodes if preserving whitespace: 82  

注解

如果源 XML 缩进,则 PreserveWhitespace 设置标志 options 会导致读取器读取源 XML 中的所有空白。 为重要且微不足道的空白创建类型 XText 节点。

如果源 XML 缩进,则不设置 PreserveWhitespace 标志 options 会导致读取器忽略源 XML 中所有无关紧要的空白。 创建 XML 树时没有任何文本节点用于无关紧要的空白。

如果源 XML 未缩进,则 PreserveWhitespace 设置 options 标志不起作用。 仍然保留大量空白,并且没有无关紧要的空白空间,这可能会导致创建更多的空白文本节点。

有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白

用于 Parse 从包含 XML 的字符串创建一个 XDocument

如果设置 SetBaseUri 标志和 SetLineInfo 标志,则会出现性能损失。

加载 XML 文档后,基 URI 和行信息会立即准确。 如果在加载文档后修改 XML 树,则基本 URI 和行信息可能毫无意义。

LINQ to XML 的加载功能基于 XmlReader. 因此,你可能会捕获重载方法引发 XmlReader.Create 的任何异常以及 XmlReader 读取和分析文档的方法。

另请参阅

适用于

Load(XmlReader, LoadOptions)

XDocument 加载 XmlReader,还可以选择设置基 URI 和保留行信息。

public:
 static System::Xml::Linq::XDocument ^ Load(System::Xml::XmlReader ^ reader, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XDocument Load (System.Xml.XmlReader reader, System.Xml.Linq.LoadOptions options);
static member Load : System.Xml.XmlReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XDocument
Public Shared Function Load (reader As XmlReader, options As LoadOptions) As XDocument

参数

reader
XmlReader

一个从其读取 XmlReader 内容的 XDocument

options
LoadOptions

一个 LoadOptions,指定是否加载基 URI 和行信息。

返回

XDocument

一个 XDocument,其中包含从指定的 XmlReader 读取的 XML。

示例

以下示例加载它从中加载的 XmlReader行信息。 然后,它会打印行信息。

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);  
    Console.WriteLine("{0}{1}{2}",  
        "Element Name".PadRight(20),  
        "Line".PadRight(5),  
        "Position");  
    Console.WriteLine("{0}{1}{2}",  
        "------------".PadRight(20),  
        "----".PadRight(5),  
        "--------");  
    foreach (XElement e in xRoot.Elements("Root").DescendantsAndSelf())  
        Console.WriteLine("{0}{1}{2}",  
            ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),  
            ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),  
            ((IXmlLineInfo)e).LinePosition);  
}  
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 XDocument = XDocument.Load(nodeReader, LoadOptions.SetLineInfo)  
    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 xRoot.Elements("Root").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  
End Using  

该示例产生下面的输出:

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

注解

通过从 DOM 文档创建 XmlNodeReader ,然后使用 XmlNodeReader 创建 XElement方法,此方法可用于在 LINQ to XML 树中创建 DOM 文档的副本。

用于 Parse 从包含 XML 的字符串创建一个 XDocument

从中XmlReader加载时设置PreserveWhitespace无效。 该 XmlReader 设置将配置为读取空格或未读取空格。 LINQ to XML 树将填充读取器表面的空白节点。 无论是否 PreserveWhitespace 设置,这都是行为。

XmlReader可能具有有效的基本 URI。 如果设置 SetBaseUri,基础 URI 将从报告 XmlReader的基础 URI 中的 XML 树中设置。

可能 XmlReader 具有有效的行信息。 如果设置 SetLineInfo,则行信息将从所报告的行信息中设置在 XML 树中 XmlReader

如果设置 SetLineInfo 标志,性能会降低。

加载 XML 文档后,行信息会立即准确。 如果在加载文档后修改 XML 树,则行信息可能毫无意义。

LINQ to XML 的加载功能基于 XmlReader。 因此,你可能会捕获重载方法和XmlReader读取和分析文档的方法引发XmlReader.Create的任何异常。

另请参阅

适用于