XDocument.Load 方法

定义

通过 URI、XDocumentURI 或 URI TextReader指定的文件创建一个新XmlReader项。

重载

名称 说明
Load(Stream)

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

Load(TextReader)

从 .XDocument. 创建一个新TextReader项。

Load(String)

从文件创建新 XDocument 项。

Load(XmlReader)

从 .XDocument. 创建一个新XmlReader项。

Load(Stream, LoadOptions)

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

Load(TextReader, LoadOptions)

从一个XDocument(可选)保留空白、设置基本 URI 和保留行信息创建新TextReader内容。

Load(String, LoadOptions)

从文件创建新 XDocument 内容,可以选择保留空格、设置基本 URI 和保留行信息。

Load(XmlReader, LoadOptions)

从 加载一个XDocumentXmlReader(可选)设置基 URI 并保留行信息。

注解

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

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

Load(Stream)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

使用指定的流创建新 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 对象,用于读取流中包含的数据。

注解

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

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

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

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

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

适用于

Load(TextReader)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从 .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

包含 的内容的 A

返回

一个 XDocument 包含指定 TextReader内容。

示例

以下示例从 /&a0> 创建文档。

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)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从文件创建新 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

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

返回

一个 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)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从 .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

包含 的内容的 A

返回

一个 XDocument 包含指定 XmlReader内容。

示例

以下示例创建 DOM 文档,从 DOM 文档创建XmlNodeReader一个XDocument,并使用 .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)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

使用指定的流创建一个新 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 对象,用于读取流中包含的数据。

注解

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

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

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

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

适用于

Load(TextReader, LoadOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从一个XDocument(可选)保留空白、设置基本 URI 和保留行信息创建新TextReader内容。

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

包含 的内容的 A

options
LoadOptions

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

返回

一个 XDocument 包含从指定的 TextReaderXML 读取的 XML。

示例

以下示例从 /&a0> 创建文档。

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

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

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

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

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

另请参阅

适用于

Load(String, LoadOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从文件创建新 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

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

options
LoadOptions

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

返回

一个 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)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

从 加载一个XDocumentXmlReader(可选)设置基 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 包含从指定的 XmlReaderXML 读取的 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

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

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

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

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

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

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

另请参阅

适用于