XmlDocument.Load 方法

定義

Stream、URL、TextReaderXmlReader 載入指定的 XML 資料。

多載

Load(Stream)

從指定的資料流載入 XML 文件。

Load(TextReader)

從指定的 TextReader 載入 XML 文件。

Load(String)

從指定的 URL 載入 XML 文件。

Load(XmlReader)

從指定的 XmlReader 載入 XML 文件。

Load(Stream)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

從指定的資料流載入 XML 文件。

public:
 virtual void Load(System::IO::Stream ^ inStream);
public virtual void Load (System.IO.Stream inStream);
abstract member Load : System.IO.Stream -> unit
override this.Load : System.IO.Stream -> unit
Public Overridable Sub Load (inStream As Stream)

參數

inStream
Stream

包含要載入之 XML 文件的資料流。

例外狀況

XML 中發生載入或剖析錯誤。 在這種情況下,會引發 FileNotFoundException

備註

注意

方法 Load 一律會保留顯著的空白字元。 屬性 PreserveWhitespace 會決定是否保留元素內容中的空白字元不重要的空白字元。 預設值為 false ;不會保留元素內容中的空白字元。

如果您想要進行驗證,您可以使用 類別和 Create 方法建立驗證 XmlReader 實例 XmlReaderSettings 。 如需詳細資訊,請參閱 XmlReader 參考頁面的<備註>一節。

此方法是檔物件模型 (DOM) 的 Microsoft 延伸模組。

這個方法會自動偵測輸入 XML (的字串格式,例如 UTF-8、ANSI 等) 。 如果您的應用程式需要知道用來讀取資料流程的編碼方式,請考慮使用 XmlTextReader 物件來讀取資料流程,然後使用 XmlTextReader.Encoding 屬性來判斷編碼方式。 如果您需要使用 XmlDocument 物件來處理 XML,您可以使用 XmlTextReader 物件來建立一個物件。 如需詳細資訊,請參閱 使用 XPathDocument 和 XmlDocument 讀取 XML 資料

另請參閱

適用於

Load(TextReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

從指定的 TextReader 載入 XML 文件。

public:
 virtual void Load(System::IO::TextReader ^ txtReader);
public virtual void Load (System.IO.TextReader txtReader);
abstract member Load : System.IO.TextReader -> unit
override this.Load : System.IO.TextReader -> unit
Public Overridable Sub Load (txtReader As TextReader)

參數

txtReader
TextReader

用於將 XML 資料送入文件中的 TextReader

例外狀況

XML 中發生載入或剖析錯誤。 在這種情況下,文件會保持空白。

範例

下列範例會使用 類別 StringReader ,將 XML 資料的 XmlDocument 字串載入 物件中。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlData = "<book xmlns:bk='urn:samples'></book>";
   doc->Load( gcnew StringReader( xmlData ) );
   
   // Create a new element and add it to the document.
   XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" );
   elem->InnerText = "fantasy";
   doc->DocumentElement->AppendChild( elem );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    string xmlData = "<book xmlns:bk='urn:samples'></book>";

    doc.Load(new StringReader(xmlData));

    // Create a new element and add it to the document.
    XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples");
    elem.InnerText = "fantasy";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main() 

    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim xmlData as string = "<book xmlns:bk='urn:samples'></book>"

    doc.Load(new StringReader(xmlData))

    ' Create a new element and add it to the document.
    Dim elem as XmlElement = doc.CreateElement("bk", "genre", "urn:samples")
    elem.InnerText = "fantasy"
    doc.DocumentElement.AppendChild(elem)

    Console.WriteLine("Display the modified XML...")
    doc.Save(Console.Out)

  end sub
end class

備註

注意

方法 Load 一律會保留顯著的空白字元。 屬性 PreserveWhitespace 會決定是否保留元素內容中的空白字元不重要的空白字元。 預設值為 false ;不會保留元素內容中的空白字元。

如果您想要進行驗證,您可以使用 類別和 Create 方法建立驗證 XmlReader 實例 XmlReaderSettings 。 如需詳細資訊,請參閱 XmlReader 參考頁面的<備註>一節。

此方法是檔物件模型 (DOM) 的 Microsoft 延伸模組。

另請參閱

適用於

Load(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

從指定的 URL 載入 XML 文件。

public:
 virtual void Load(System::String ^ filename);
public virtual void Load (string filename);
abstract member Load : string -> unit
override this.Load : string -> unit
Public Overridable Sub Load (filename As String)

參數

filename
String

包含要載入之 XML 文件的檔案的 URL。 這個 URL 可以是本機檔案,也可以是 HTTP URL (網址)。

例外狀況

XML 中發生載入或剖析錯誤。 在這種情況下,會引發 FileNotFoundException

filename 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。

filenamenull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

開啟檔案時發生 I/O 錯誤。

filename 指定了唯讀的檔案。

-或-

這個作業在目前平台不受支援。

-或-

filename 指定了目錄。

-或-

呼叫端沒有必要的權限。

找不到 filename 指定的檔案。

filename 格式無效。

呼叫端沒有必要的權限。

備註

注意

方法 Load 一律會保留顯著的空白字元。 屬性 PreserveWhitespace 會決定是否保留元素內容中的空白字元不重要的空白字元。 預設值為 false ;不會保留元素內容中的空白字元。

如果您想要進行驗證,您可以使用 類別和 Create 方法建立驗證 XmlReader 實例 XmlReaderSettings 。 如需詳細資訊,請參閱 XmlReader 參考頁面的<備註>一節。

此方法是檔物件模型 (DOM) 的 Microsoft 延伸模組。

另請參閱

適用於

Load(XmlReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

從指定的 XmlReader 載入 XML 文件。

public:
 virtual void Load(System::Xml::XmlReader ^ reader);
public virtual void Load (System.Xml.XmlReader reader);
abstract member Load : System.Xml.XmlReader -> unit
override this.Load : System.Xml.XmlReader -> unit
Public Overridable Sub Load (reader As XmlReader)

參數

reader
XmlReader

用於將 XML 資料送入文件中的 XmlReader

例外狀況

XML 中發生載入或剖析錯誤。 在這種情況下,文件會保持空白。

範例

下列範例會將檔案的最後一個書籍節點 books.xml 載入 XML 檔中。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   
   //Load the document with the last book node.
   XmlTextReader^ reader = gcnew XmlTextReader( "books.xml" );
   reader->WhitespaceHandling = WhitespaceHandling::None;
   reader->MoveToContent();
   reader->Read();
   reader->Skip(); //Skip the first book.
   reader->Skip(); //Skip the second book.
   doc->Load( reader );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Load the document with the last book node.
    XmlTextReader reader = new XmlTextReader("books.xml");
    reader.WhitespaceHandling = WhitespaceHandling.None;
    reader.MoveToContent();
    reader.Read();
    reader.Skip(); //Skip the first book.
    reader.Skip(); //Skip the second book.
    doc.Load(reader);

    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        
        'Load the document with the last book node.
        Dim reader As New XmlTextReader("books.xml")
        reader.WhitespaceHandling = WhitespaceHandling.None
        reader.MoveToContent()
        reader.Read()
        reader.Skip() 'Skip the first book.
        reader.Skip() 'Skip the second book.
        doc.Load(reader)
        
        doc.Save(Console.Out)
    End Sub
End Class

此範例會使用 檔案 books.xml ,作為輸入。

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

備註

注意

方法 Load 一律會保留顯著的空白字元。 屬性 PreserveWhitespace 會決定是否保留元素內容中的空白字元不重要的空白字元。 預設值為 false ;不會保留元素內容中的空白字元。

如果讀取器處於初始狀態 (ReadState =ReadState.Initial) ,則會取用讀取器的完整內容, Load 並從找到的內容建置 DOM。

如果讀取器已經位於深度 「n」 的某個節點上,這個方法會將該節點和所有後續同層級載入到關閉深度 「n」 的結束標籤。 這具有下列結果。

如果目前的節點及其同層級看起來如下:

<!--comment--><element1>one</element1><element2>two</element2>

Load 會擲回例外狀況,因為檔不能有兩個根層級元素。 如果目前的節點及其同層級看起來如下:

<!--comment--><?process instruction?><!--comment--></endtag>

Load 成功,但您有不完整的 DOM 樹狀結構,因為沒有根層級元素。 儲存檔之前,您必須先新增根層級元素,否則 Save 會擲回例外狀況。

如果讀取器位於檔根層級不正確分葉節點上,例如空白字元或屬性節點,則讀取器會繼續讀取,直到它位於可用於根目錄的節點上為止。 此時文件會開始載入。

如果您想要進行驗證,您可以使用 類別和 Create 方法建立驗證 XmlReader 實例 XmlReaderSettings 。 如需詳細資訊,請參閱 XmlReader 參考頁面的<備註>一節。

此方法是檔物件模型 (DOM) 的 Microsoft 延伸模組。

另請參閱

適用於