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,並保留行資訊。

備註

您可以使用此方法的其中一個多載,從檔案、 TextReaderXmlReader 載入 XDocument

若要從包含 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.Create 回的任何例外狀況,以及 XmlReader 讀取和剖析檔的方法。

如果您必須修改 XmlReaderSettings ,請遵循下列步驟:

  1. XmlReader藉由呼叫其中一個採用 XmlReaderSettings 做為參數的多 Create 載來建立 。

  2. XmlReader將 傳遞至 的其中一個 Load 多載,該多載 XDocument 採用 XmlReader 做為參數。

適用於

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 內容。

範例

下列範例會從 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 內容。

範例

下列範例會建立 DOM 檔、從 DOM 檔建立 XmlNodeReader 、使用 XmlNodeReader 建立 XDocument

// 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藉由呼叫其中一個採用 XmlReaderSettings 做為參數的多 Create 載來建立 。

  2. XmlReader將 傳遞至 的其中一個 Load 多載,該多載 XDocument 採用 XmlReader 做為參數。

適用於

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,其指定泛空白字元 (White Space) 的行為,以及是否要載入基底 URI 和行資訊。

傳回

XDocument

XDocument,包含從指定之 TextReader 讀取的 XML。

範例

下列範例會從 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,在 中 options 設定 PreserveWhitespace 旗標會讓讀取器讀取來源 XML 中的所有空白字元。 系統會針對重大且不重要的空白字元建立類型的 XText 節點。

如果來源 XML 縮排,未在 中 options 設定 PreserveWhitespace 旗標,則讀取器會忽略來源 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,其指定泛空白字元 (White Space) 的行為,以及是否要載入基底 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,在 中 options 設定 PreserveWhitespace 旗標會讓讀取器讀取來源 XML 中的所有空白字元。 系統會針對重大且不重要的空白字元建立類型的 XText 節點。

如果來源 XML 縮排,未在 中 options 設定 PreserveWhitespace 旗標,則讀取器會忽略來源 XML 中所有無意義的空白字元。 XML 樹狀結構會建立,而不會有任何文位元組點用於不重要的空白字元。

如果來源 XML 未縮排,則 PreserveWhitespace 設定 中的 options 旗標沒有任何作用。 仍然會保留重要的空白字元,而且沒有不重要的空白字元範圍,可能會導致建立更多空白字元文位元組點。

如需詳細資訊,請參閱 在載入或剖析 XML 時保留空白字元 ,以及在 序列化時保留空白字元

使用 Parse 從包含 XML 的字串建立 XDocument

如果您設定 SetBaseUriSetLineInfo 旗標,效能會受到負面影響。

載入 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 ,則行資訊將會從 所 XmlReader 報告的行資訊中設定在 XML 樹狀結構中。

如果您設定 SetLineInfo 旗標,效能會受到負面影響。

載入 XML 檔之後,行資訊會立即正確。 如果您在載入檔之後修改 XML 樹狀結構,行資訊可能會變成無意義。

LINQ to XML的載入功能建置在 上 XmlReader 。 因此,您可能會攔截多載方法擲回 XmlReader.Create 的任何例外狀況,以及 XmlReader 讀取和剖析檔的方法。

另請參閱

適用於