DataSet.ReadXml メソッド
XML スキーマとデータを DataSet に読み込みます。
オーバーロードの一覧
指定した System.IO.Stream を使用して、XML スキーマとデータを DataSet に読み込みます。
[Visual Basic] Overloads Public Function ReadXml(Stream) As XmlReadMode
指定したファイルを使用して、XML スキーマとデータを DataSet に読み込みます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function ReadXml(String) As XmlReadMode
指定した System.IO.TextReader を使用して、XML スキーマとデータを DataSet に読み込みます。
[Visual Basic] Overloads Public Function ReadXml(TextReader) As XmlReadMode
[JScript] public function ReadXml(TextReader) : XmlReadMode;
指定した System.Xml.XmlReader を使用して、XML スキーマとデータを DataSet に読み込みます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function ReadXml(XmlReader) As XmlReadMode
指定した System.IO.Stream と XmlReadMode を使用して、XML スキーマとデータを DataSet に読み込みます。
[Visual Basic] Overloads Public Function ReadXml(Stream, XmlReadMode) As XmlReadMode
[JScript] public function ReadXml(Stream, XmlReadMode) : XmlReadMode;
指定したファイルと XmlReadMode を使用して、XML スキーマとデータを DataSet に読み込みます。
[Visual Basic] Overloads Public Function ReadXml(String, XmlReadMode) As XmlReadMode
[JScript] public function ReadXml(String, XmlReadMode) : XmlReadMode;
指定した System.IO.TextReader と XmlReadMode を使用して、XML スキーマとデータを DataSet に読み込みます。
[Visual Basic] Overloads Public Function ReadXml(TextReader, XmlReadMode) As XmlReadMode
[C++] public: XmlReadMode ReadXml(TextReader*, XmlReadMode);
[JScript] public function ReadXml(TextReader, XmlReadMode) : XmlReadMode;
指定した System.Xml.XmlReader と XmlReadMode を使用して、XML スキーマとデータを DataSet に読み込みます。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function ReadXml(XmlReader, XmlReadMode) As XmlReadMode
[JScript] public function ReadXml(XmlReader, XmlReadMode) : XmlReadMode;
使用例
[Visual Basic, C#, C++] 初めに 1 つの DataTable 、2 列、および 10 行で単純な DataSet を作成する例を次に示します。 DataSet のスキーマとデータは、 WriteXml メソッドの呼び出しによってディスクに書き込みます。2 番目の DataSet を作成し、 ReadXml メソッドを使用して、スキーマとデータを格納します。
[Visual Basic, C#, C++] メモ ここでは、ReadXml のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
' Create a DataSet with one table and two columns.
Dim OriginalDataSet As New DataSet("myDataSet")
OriginalDataSet.Namespace = "NetFrameWork"
Dim myTable As New DataTable("myTable")
Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
c1.AutoIncrement = True
Dim c2 As New DataColumn("item")
myTable.Columns.Add(c1)
myTable.Columns.Add(c2)
OriginalDataSet.Tables.Add(myTable)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = myTable.NewRow()
newRow("item") = "item " + i.ToString()
myTable.Rows.Add(newRow)
Next i
OriginalDataSet.AcceptChanges()
' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(OriginalDataSet, "Original DataSet")
' Write the XML schema and data to file with FileStream.
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter _
(fsWriteXml, System.Text.Encoding.Unicode)
' Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter)
' Close the FileStream.
fsWriteXml.Close()
' Dispose of the original DataSet.
OriginalDataSet.Dispose()
' Create a new DataSet.
Dim newDataSet As New DataSet("New DataSet")
' Read the XML document back in.
' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
' Close the XmlTextReader
myXmlReader.Close()
' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(newDataSet, "New DataSet")
End Sub
Private Sub PrintValues(ds As DataSet, label As String)
Console.WriteLine(ControlChars.Cr + label)
Dim t As DataTable
Dim r As DataRow
Dim c As DataColumn
For Each t In ds.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r In t.Rows
For Each c In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
[C#]
private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the XML schema and data to file with FileStream.
string xmlFilename = "myXmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml.Close();
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
[C++]
private:
void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet* OriginalDataSet = new DataSet(S"myDataSet");
OriginalDataSet->Namespace= S"NetFrameWork";
DataTable* myTable = new DataTable(S"myTable");
DataColumn* c1 = new DataColumn(S"id", Type::GetType(S"System.Int32"));
c1->AutoIncrement= true;
DataColumn* c2 = new DataColumn(S"item");
myTable->Columns->Add(c1);
myTable->Columns->Add(c2);
OriginalDataSet->Tables->Add(myTable);
// Add ten rows.
DataRow* newRow;
for(int i = 0; i < 10; i++){
newRow = myTable->NewRow();
newRow->Item[S"item"]= String::Format( S"item {0}", __box(i));
myTable->Rows->Add(newRow);
}
OriginalDataSet->AcceptChanges();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(OriginalDataSet, S"Original DataSet");
// Write the XML schema and data to file with FileStream.
String* xmlFilename = S"myXmlDocument.xml";
// Create FileStream
System::IO::FileStream* fsWriteXml = new System::IO::FileStream
(xmlFilename, System::IO::FileMode::Create);
// Create an XmlTextWriter to write the file.
System::Xml::XmlTextWriter* xmlWriter = new System::Xml::XmlTextWriter
(fsWriteXml, System::Text::Encoding::Unicode);
// Use WriteXml to write the document.
OriginalDataSet->WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml->Close();
// Dispose of the original DataSet.
OriginalDataSet->Dispose();
// Create a new DataSet.
DataSet* newDataSet = new DataSet(S"New DataSet");
// Read the XML document back in.
// Create new FileStream to read schema with.
System::IO::FileStream* fsReadXml = new System::IO::FileStream
(xmlFilename, System::IO::FileMode::Open);
// Create an XmlTextReader to read the file.
System::Xml::XmlTextReader* myXmlReader =
new System::Xml::XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet->ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader->Close();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,S"New DataSet");
}
void PrintValues(DataSet* ds, String* label){
Console::WriteLine(S"\n{0}", label);
System::Collections::IEnumerator* myEnum = ds->Tables->GetEnumerator();
while (myEnum->MoveNext())
{
DataTable* t = __try_cast<DataTable*>(myEnum->Current);
Console::WriteLine(S"TableName: {0}", t->TableName);
System::Collections::IEnumerator* myEnum1 = t->Rows->GetEnumerator();
while (myEnum1->MoveNext())
{
DataRow* r = __try_cast<DataRow*>(myEnum1->Current);
System::Collections::IEnumerator* myEnum2 = t->Columns->GetEnumerator();
while (myEnum2->MoveNext())
{
DataColumn* c = __try_cast<DataColumn*>(myEnum2->Current);
Console::Write(S"\t {0}", r->Item[c] );
}
Console::WriteLine();
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。