XDocument.Load 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 URI 指定的檔案、XDocument從 、 或 TextReader建立一個新的XmlReader檔案。
多載
| 名稱 | Description |
|---|---|
| Load(Stream) |
透過指定的串流建立一個新 XDocument 實例。 |
| Load(TextReader) |
從 XDocument. 建立一個新的 TextReader 。 |
| Load(String) |
從檔案建立新 XDocument 檔案。 |
| Load(XmlReader) | |
| Load(Stream, LoadOptions) |
透過使用指定的串流建立新 XDocument 實例,並可選擇保留空白空間、設定基礎 URI 並保留行資訊。 |
| Load(TextReader, LoadOptions) |
從 建立XDocument新TextReader檔案,可選擇保留空白、設定基礎 URI 並保留行資訊。 |
| Load(String, LoadOptions) |
從檔案建立新 XDocument 檔案,可選擇保留空白、設定基礎 URI 並保留行資訊。 |
| Load(XmlReader, LoadOptions) |
備註
利用此方法的其中一個超載,你可以從檔案載入 a XDocument 、a TextReader、 或 XmlReader。
若要從包含 XML 的字串建立 , XDocument 請使用 Parse。
Load(Stream)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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 take 的 LoadOptions 超載作為參數。
LINQ 轉 XML 的載入功能建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
如果你需要修改 XmlReaderSettings,請遵循以下步驟:
透過呼叫XmlReader一個以參數為Create參數的超載來建立 。XmlReaderSettings
適用於
Load(TextReader)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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
TextReader A 包含 的內容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 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Load(String)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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
參數
傳回
一個 XDocument 包含指定檔案內容的
範例
以下範例說明如何從檔案載入 。XDocument
此範例使用以下 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 以建立 XDocument 包含 XML 的字串。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Load(XmlReader)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
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
參數
傳回
一個 XDocument 包含指定 XmlReader內容的 。
範例
以下範例建立 DOM 文件,從 DOM 文件建立 XmlNodeReader ,使用 XDocumentXmlNodeReader。
// 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 轉 XML 樹狀的 DOM 文件副本。 要做到這點,你要從 DOM 文件建立一個 XmlNodeReader ,然後用 來 XmlNodeReader 建立一個 XDocument。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Load(Stream, LoadOptions)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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 和行資訊的 A。
傳回
一個 XDocument 讀取串流中資料的物件。
備註
LINQ 轉 XML 的載入功能建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
如果你需要修改 XmlReaderSettings,請遵循以下步驟:
透過呼叫XmlReader其中一個以參數為Create參數的超載來建立 。XmlReaderSettings
適用於
Load(TextReader, LoadOptions)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
從 建立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 A 包含 的內容XDocument。
- options
- LoadOptions
A LoadOptions 指定空白空間行為,以及是否載入基底 URI 和行資訊。
傳回
一個 XDocument 包含從指定 TextReader.
範例
以下範例由 建立一份文件 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 以建立 XElement 包含 XML 的字串。
從 SetBaseUri載入時 設定TextReader不適用。
如果你設旗 SetLineInfo 子,會有績效懲罰。
行資訊在載入 XML 文件後立即準確。 如果你在載入文件後修改 XML 樹,該行資訊可能會變得毫無意義。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Load(String, LoadOptions)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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
參數
- options
- LoadOptions
A LoadOptions 指定空白空間行為,以及是否載入基底 URI 和行資訊。
傳回
一個 XDocument 包含指定檔案內容的
範例
以下範例說明如何從檔案載入 。XDocument
此範例使用以下 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 以建立 XDocument 包含 XML 的字串。
如果你設定了 和 SetBaseUriSetLineInfo 旗標,會有績效懲罰。
基礎 URI 與行資訊在載入 XML 文件後立即準確。 如果你在載入文件後修改 XML 樹,基礎 URI 和行資訊可能會變得毫無意義。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Load(XmlReader, LoadOptions)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
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
參數
- options
- LoadOptions
一個 LoadOptions 指定是否載入基底 URI 和行資訊的 A。
傳回
範例
以下範例載入從 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 轉 XML 樹中建立 DOM 文件的副本。
使用 Parse 以建立 XDocument 包含 XML 的字串。
從 PreserveWhitespace載入時 設定XmlReader不適用。 它們 XmlReader 會被設定成要麼讀取空白空間,要麼不讀。 LINQ 轉 XML 樹會填充讀取器所呈現的空白節點。 不管設定 PreserveWhitespace 是否設定,這個行為都會持續發生。
他們 XmlReader 可能有有效的基礎 URI,也可能沒有。 如果你設定 SetBaseUri,基礎 URI 會從 由 報告的基礎 URI XmlReader在 XML 樹中被設定。
他們 XmlReader 可能有有效的線路資訊,也可能沒有。 如果你設定 SetLineInfo,行資訊會在 XML 樹中由 所報告 XmlReader的行資訊設定。
如果你設旗 SetLineInfo 子,會有績效懲罰。
行資訊在載入 XML 文件後立即準確。 如果你在載入文件後修改 XML 樹,該行資訊可能會變得毫無意義。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。