XmlDocument.Load 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 URL Stream、URL、 TextReadera 或 an XmlReader中加载指定的 XML 数据。
重载
| 名称 | 说明 |
|---|---|
| Load(Stream) |
从指定的流加载 XML 文档。 |
| Load(TextReader) |
从指定的 TextReader.. |
| Load(String) |
从指定的 URL 加载 XML 文档。 |
| Load(XmlReader) |
从指定的 XmlReader.. |
Load(Stream)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- 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 中存在加载或分析错误。 在这种情况下,将引发 a 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
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
从指定的 TextReader..
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
TextReader用于将 XML 数据馈送到文档中。
例外
XML 中存在加载或分析错误。 在这种情况下,文档保持为空。
示例
以下示例使用 StringReader 类将 XML 数据的字符串加载到对象中 XmlDocument 。
using System;
using System.IO;
using System.Xml;
public class Sample1
{
public static void CreateElementExample()
{
// Create the XmlDocument.
XmlDocument doc = new();
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
- 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(Web 地址)。
例外
XML 中存在加载或分析错误。 在这种情况下,将引发 a FileNotFoundException 。
filename 是一个零长度字符串,仅包含空格,或包含一个或多个无效字符,由 InvalidPathChars该字符串定义。
filename 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
打开文件时出现 I/O 错误。
找不到在其中 filename 指定的文件。
filename 格式无效。
调用方没有所需的权限。
注解
注释
该方法 Load 始终保留大量空白。 该 PreserveWhitespace 属性确定是否保留元素内容中空白的微不足道的空格。 默认值为 false;元素内容中的空格不会保留。
如果要进行验证,可以使用类和Create方法创建验证XmlReader实例XmlReaderSettings。 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。
此方法是文档对象模型(DOM)的Microsoft扩展。
另请参阅
适用于
Load(XmlReader)
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
- Source:
- XmlDocument.cs
从指定的 XmlReader..
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
XmlReader用于将 XML 数据馈送到文档中。
例外
XML 中存在加载或分析错误。 在这种情况下,文档保持为空。
示例
以下示例将文件的最后一个书籍节点 books.xml 加载到 XML 文档中。
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扩展。