次の方法で共有


XmlDataDocument.Load メソッド (XmlReader)

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

Overrides Overloads Public Sub Load( _
   ByVal reader As XmlReader _)
[C#]
public override void Load(XmlReaderreader);
[C++]
public: void Load(XmlReader* reader);
[JScript]
public override function Load(
   reader : XmlReader);

パラメータ

  • reader
    読み込む XML ドキュメントを格納している XmlReader

例外

例外の種類 条件
NotSupportedException 読み込まれている XML にエンティティ参照が含まれているため、リーダーがエンティティを解決できません。

解説

XmlDataDocument がエンティティ参照の作成をサポートしていません。データ ソースにエンティティ参照が含まれている場合は、 EntityHandling プロパティを EntityHandling.ExpandEntities (既定の動作) に設定して XmlValidatingReader を作成し、 XmlValidatingReader を Load メソッドに渡す必要があります。 XmlValidatingReader を使用しない場合は、Load メソッドは例外をスローします。

Load メソッドは、有意な空白を常に保存します。 PreserveWhitespace プロパティは、空白が保存されるかどうかを決定します。既定値は false で、空白は保存されません。

リーダーが初期状態にある場合、つまり ReadState=ReadState.Initial の場合、Load はリーダーの内容全体を使用して、検索結果から DOM を構築します。

リーダーが深さ "n" のあるノード上に既に配置されている場合、このメソッドは、そのノードおよび深さ "n" で閉じている終了タグまでのすべての後続の兄弟を読み込みます。結果は次のとおりです。

現在のノードとそれに続く兄弟が、次の例に類似する場合、

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

1 つのドキュメントに 2 つのルート レベル要素を設定することはできないため、Load は例外をスローします。現在のノードとそれに続く兄弟が、次の例に類似する場合、

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

Load は成功しますが、ルート レベルの要素がないため、結果は不完全な DOM ツリーになります。ドキュメントを保存する前にルート レベルの要素を追加する必要があります。追加しない場合、 Save メソッドは例外をスローします。

リーダーが、空白や属性ノードなど、ドキュメントのルート レベルには無効なリーフ ノードに配置されている場合、リーダーは、ルートとして使用できるノード上に配置されるまで読み込みを続けます。ドキュメントは、この時点で読み込みを開始します。

使用例

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

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

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

参照

XmlDataDocument クラス | XmlDataDocument メンバ | System.Xml 名前空間 | XmlDataDocument.Load オーバーロードの一覧