次の方法で共有


XmlDataDocument.Load メソッド

指定したデータ ソースを使用して XmlDataDocument を読み込み、読み込んだデータと DataSet を同期します。

メモ   XML データをリレーショナル形式で表示するには、まずデータ マップに使用するスキーマを指定する必要があります。これは、 ReadXmlSchema メソッドを呼び出すか、 DataSet 内のテーブルと列を手動で作成することによって行うことができます。この手順は、Load を呼び出す前に行う必要があります。

XmlDataDocument がエンティティ参照の作成をサポートしていません。データにエンティティ参照が含まれている場合は、Load メソッドは任意のエンティティ参照を解決および展開します。ただし、引数として XmlReader を取る Load オーバーロードを使用している場合は、エンティティを解決できる XmlReader を指定する必要があります。

オーバーロードの一覧

指定したストリームから XmlDataDocument を読み込みます。

[Visual Basic] Overloads Overrides Public Sub Load(Stream)

[C#] public override void Load(Stream);

[C++] public: void Load(Stream*);

[JScript] public override function Load(Stream);

指定した URL を使用して、 XmlDataDocument を読み込みます。

[Visual Basic] Overloads Overrides Public Sub Load(String)

[C#] public override void Load(string);

[C++] public: void Load(String*);

[JScript] public override function Load(String);

指定した TextReader から XmlDataDocument を読み込みます。

[Visual Basic] Overloads Overrides Public Sub Load(TextReader)

[C#] public override void Load(TextReader);

[C++] public: void Load(TextReader*);

[JScript] public override function Load(TextReader);

指定した XmlReader から XmlDataDocument を読み込みます。

[Visual Basic] Overloads Overrides Public Sub Load(XmlReader)

[C#] public override void Load(XmlReader);

[C++] public: void Load(XmlReader*);

[JScript] public override function Load(XmlReader);

使用例

[Visual Basic, C#, C++] DataSet メソッドを使用して書籍の価格を変更する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、Load のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.Data
Imports System.Xml

public class Sample

  public shared sub Main()

      'Create an XmlDataDocument.
      Dim doc as XmlDataDocument = new XmlDataDocument()

      'Load the schema.
      doc.DataSet.ReadXmlSchema("store.xsd") 
 
      'Load the XML data.
      Dim reader as XmlTextReader = new XmlTextReader("2books.xml")
      reader.MoveToContent() 'Moves the reader to the root node.
      doc.Load(reader)
        
     'Change the price on the first book imports the DataSet methods.
     Dim books as DataTable = doc.DataSet.Tables.Item("book")
     books.Rows.Item(0).Item("price") = "12.95"
        
     Console.WriteLine("Display the modified XML data...")
     doc.Save(Console.Out)

  end sub
end class

[C#] 
using System;
using System.Data;
using System.Xml;
public class Sample {
    public static void Main() {
        // Create an XmlDataDocument.
        XmlDataDocument doc = new XmlDataDocument();
        
        // Load the schema file.
        doc.DataSet.ReadXmlSchema("store.xsd");
        
        // Load the XML data.
        XmlTextReader reader = new XmlTextReader("2books.xml");
        reader.MoveToContent(); // Moves the reader to the root node.
        doc.Load(reader);
        
        // Update the price on the first book using the DataSet methods.
        DataTable books = doc.DataSet.Tables["book"];
        books.Rows[0]["price"] = "12.95";
        
        Console.WriteLine("Display the modified XML data...");
        doc.Save(Console.Out);
    }
} // End class

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Xml;

int main() 
{
   // Create an XmlDataDocument.
   XmlDataDocument* doc = new XmlDataDocument();

   // Load the schema file.
   doc->DataSet->ReadXmlSchema(S"store.xsd");

   // Load the XML data.
   XmlTextReader* reader = new XmlTextReader(S"2books.xml");
   reader->MoveToContent(); // Moves the reader to the root node.
   doc->Load(reader);

   // Update the price on the first book using the DataSet methods.
   DataTable* books = doc->DataSet->Tables->get_Item( S"book" );
   books->Rows->get_Item(0)->set_Item(S"price", S"12.95");

   Console::WriteLine( S"Display the modified XML data...");
   doc->Save(Console::Out);
}

[Visual Basic, C#, C++] この例では、次の 2 つの入力ファイルを使用します。

[Visual Basic, C#, C++] 2books.xml

<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

[Visual Basic, C#, C++] store.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

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

参照

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