XmlDocument.LoadXml 方法

从指定的字符串加载 XML 文档。

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Overridable Sub LoadXml ( _
    xml As String _
)
用法
Dim instance As XmlDocument
Dim xml As String

instance.LoadXml(xml)
public virtual void LoadXml (
    string xml
)
public:
virtual void LoadXml (
    String^ xml
)
public void LoadXml (
    String xml
)
public function LoadXml (
    xml : String
)

参数

  • xml
    包含要加载的 XML 文档的字符串。

异常

异常类型 条件

XmlException

XML 中有加载或分析错误。这种情况下,文档保持为空。

备注

默认情况下,LoadXml 方法既不保留空白,也不保留有意义的空白。

此方法不执行 DTD 或架构验证。如果希望验证发生,请使用 Load 方法并向其传递一个 XmlValidatingReader。有关加载时间验证的示例,请参见 XmlDocument

该方法是文档对象模型 (DOM) 的 Microsoft 扩展。

示例

下面的示例将 XML 加载到 XmlDocument 对象中,然后将其保存到外部文件中。

Imports System
Imports System.Xml

public class Sample 

  public shared sub Main() 
 
    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<item><name>wrench</name></item>")

   ' Add a price element.
   Dim newElem as XmlElement = doc.CreateElement("price")
   newElem.InnerText = "10.95"
   doc.DocumentElement.AppendChild(newElem)

    ' Save the document to a file and auto-indent the output.
    Dim writer as XmlTextWriter = new XmlTextWriter("data.xml",nothing)
    writer.Formatting = Formatting.Indented
    doc.Save(writer)
  end sub
end class
using System;
using System.Xml;

public class Sample {

  public static void Main() {
 
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");

   // Add a price element.
   XmlElement newElem = doc.CreateElement("price");
   newElem.InnerText = "10.95";
   doc.DocumentElement.AppendChild(newElem);

    // Save the document to a file and auto-indent the output.
    XmlTextWriter writer = new XmlTextWriter("data.xml",null);
    writer.Formatting = Formatting.Indented;
    doc.Save(writer);
  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<item><name>wrench</name></item>" );
   
   // Add a price element.
   XmlElement^ newElem = doc->CreateElement( "price" );
   newElem->InnerText = "10.95";
   doc->DocumentElement->AppendChild( newElem );
   
   // Save the document to a file and auto-indent the output.
   XmlTextWriter^ writer = gcnew XmlTextWriter( "data.xml", nullptr );
   writer->Formatting = Formatting::Indented;
   doc->Save( writer );
}
import System.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // Create the XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<item><name>wrench</name></item>");

        // Add a price element.
        XmlElement newElem = doc.CreateElement("price");
        newElem.set_InnerText("10.95");
        doc.get_DocumentElement().AppendChild(newElem);

        // Save the document to a file and auto-indent the output.
        XmlTextWriter writer = new XmlTextWriter("data.xml", null);
        writer.set_Formatting(Formatting.Indented);
        doc.Save(writer);
    } //main
} //Sample

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

XmlDocument 类
XmlDocument 成员
System.Xml 命名空间
PreserveWhitespace