DataTables
のDataSet
を歩き、XMLにデータを書き込むという簡単な例を以下に示します。これにはさまざまなスキーマがありますので、出力を見た目にするためにこのコードを少し修正する必要があります。
Imports System.Xml
Module Module1
Sub Main()
Dim dataset As New DataSet
dataset.Tables.Add(CreateDataTable("Table1", 5))
dataset.Tables.Add(CreateDataTable("Table2", 8))
Using writer As XmlWriter = XmlWriter.Create("test.xml",
New XmlWriterSettings() With {.Indent = True})
writer.WriteStartDocument()
writer.WriteStartElement("Dataset")
For Each table As DataTable In dataset.Tables
writer.WriteStartElement("Table")
writer.WriteAttributeString("Name", table.TableName)
For Each row As DataRow In table.Rows
writer.WriteStartElement("Row")
For Each column As DataColumn In table.Columns
writer.WriteStartElement(column.ColumnName)
writer.WriteValue(row(column))
writer.WriteEndElement()
Next
writer.WriteEndElement()
Next
writer.WriteEndElement()
Next
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
End Sub
Private Function CreateDataTable(name As String, rows As Integer) As DataTable
Dim table As New DataTable(name)
table.Columns.Add("Col1", GetType(String))
table.Columns.Add("Col2", GetType(String))
table.Columns.Add("Col3", GetType(String))
table.Columns.Add("Col4", GetType(String))
For r As Integer = 1 To rows
Dim row As DataRow = table.NewRow()
table.Rows.Add(row)
For i As Integer = 1 To table.Columns.Count
row(i - 1) = $"Row {r}, column {i}"
Next
Next
Return table
End Function
End Module
XmlWriter
は、ステートマシンとして内部的に働くあなたが行くように、あなたは、進行中の要素を終えることWriteCloseElement
ジェネリックを呼び出し、要素、属性、および値を書き込みます。
これは、ドキュメント要素と「データセット」という名前のルート要素を作成するだけです。次に、各DataTable
をループし、テーブルの "Name"属性を持つそれぞれの "Table"要素を記述します。次に、DataTable
の各DataRow
をループし、「行」要素を書き込みます。各 "行"要素内では、DataTable
のすべてのDataColumn
をループし、DataColumn
と同じ名前の新しい要素を書き込みます。次に、そのDataColumn
の値をそのDataRow
に書き込みます。ループを終了すると、各要素を閉じます。
それは、この出力を生成します
<?xml version="1.0" encoding="utf-8"?>
<Dataset>
<Table Name="Table1">
<Row>
<Col1>Row 1, column 1</Col1>
<Col2>Row 1, column 2</Col2>
<Col3>Row 1, column 3</Col3>
<Col4>Row 1, column 4</Col4>
</Row>
<Row>
<Col1>Row 2, column 1</Col1>
<Col2>Row 2, column 2</Col2>
<Col3>Row 2, column 3</Col3>
<Col4>Row 2, column 4</Col4>
</Row>
<Row>
<Col1>Row 3, column 1</Col1>
<Col2>Row 3, column 2</Col2>
<Col3>Row 3, column 3</Col3>
<Col4>Row 3, column 4</Col4>
</Row>
<Row>
<Col1>Row 4, column 1</Col1>
<Col2>Row 4, column 2</Col2>
<Col3>Row 4, column 3</Col3>
<Col4>Row 4, column 4</Col4>
</Row>
<Row>
<Col1>Row 5, column 1</Col1>
<Col2>Row 5, column 2</Col2>
<Col3>Row 5, column 3</Col3>
<Col4>Row 5, column 4</Col4>
</Row>
</Table>
<Table Name="Table2">
<Row>
<Col1>Row 1, column 1</Col1>
<Col2>Row 1, column 2</Col2>
<Col3>Row 1, column 3</Col3>
<Col4>Row 1, column 4</Col4>
</Row>
<Row>
<Col1>Row 2, column 1</Col1>
<Col2>Row 2, column 2</Col2>
<Col3>Row 2, column 3</Col3>
<Col4>Row 2, column 4</Col4>
</Row>
<Row>
<Col1>Row 3, column 1</Col1>
<Col2>Row 3, column 2</Col2>
<Col3>Row 3, column 3</Col3>
<Col4>Row 3, column 4</Col4>
</Row>
<Row>
<Col1>Row 4, column 1</Col1>
<Col2>Row 4, column 2</Col2>
<Col3>Row 4, column 3</Col3>
<Col4>Row 4, column 4</Col4>
</Row>
<Row>
<Col1>Row 5, column 1</Col1>
<Col2>Row 5, column 2</Col2>
<Col3>Row 5, column 3</Col3>
<Col4>Row 5, column 4</Col4>
</Row>
<Row>
<Col1>Row 6, column 1</Col1>
<Col2>Row 6, column 2</Col2>
<Col3>Row 6, column 3</Col3>
<Col4>Row 6, column 4</Col4>
</Row>
<Row>
<Col1>Row 7, column 1</Col1>
<Col2>Row 7, column 2</Col2>
<Col3>Row 7, column 3</Col3>
<Col4>Row 7, column 4</Col4>
</Row>
<Row>
<Col1>Row 8, column 1</Col1>
<Col2>Row 8, column 2</Col2>
<Col3>Row 8, column 3</Col3>
<Col4>Row 8, column 4</Col4>
</Row>
</Table>
</Dataset>
「XmlWriter」に出力している間に、テーブルの行に内部ループがあり、フィールドにもう1つ内側のループがあります。 –
'最初に投稿する.'これはあなたが[質問]を読んで[ツアー]をしておくべきだったことを意味します。残りの部分は、実際にXMLをどのように生成しているかによって異なります。 DataGridViewは、私が知る限り、その能力を持っていません。データソースはありますが、 – Plutonix
@BradleyUffner私はXmlWriterを調べるために最善を尽くしましたが、ちょっと混乱しています。アイデアは私は行ごとに行かれ、各行をXMLファイルに書き込むのですか? –