次の方法で共有


XmlDocument.LoadXml メソッド

指定した文字列から XML ドキュメントを読み込みます。

Public Overridable Sub LoadXml( _
   ByVal xml As String _)
[C#]
public virtual void LoadXml(stringxml);
[C++]
public: virtual void LoadXml(String* xml);
[JScript]
public function LoadXml(
   xml : String);

パラメータ

  • xml
    読み込む XML ドキュメントを格納している文字列。

例外

例外の種類 条件
XmlException XML 内に読み込みエラーまたは解析エラーがあります。この場合、ドキュメントは空のままです。

解説

既定では、 LoadXml メソッドは空白も有意な空白も保存しません。このメソッドは、DTD 検証またはスキーマ検証を実行しません。検証を実行する場合は、 Load メソッドを使用し、そのメソッドに XmlValidatingReader を渡します。読み込み時検証の例については、「 XmlDocument 」を参照してください。

このメソッドは、DOM (Document Object Model) に対する Microsoft 拡張機能です。

使用例

[Visual Basic, C#, C++] 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

[C#] 
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);
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;

int main()
{
    // Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<item><name>wrench</name></item>");

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

    // Save the document to a file and auto-indent the output.
    XmlTextWriter* writer = new XmlTextWriter(S"data.xml",0);
    writer->Formatting = Formatting::Indented;
    doc->Save(writer);
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlDocument クラス | XmlDocument メンバ | System.Xml 名前空間 | PreserveWhitespace