loading an xmldocument object

  • Thread starter Thread starter Roger
  • Start date Start date
Roger said:
is that a way to load an xmldocument object into a dataset object?

Yes. The steps below assume that the "xmldocument object" consists of data
elements that can be used to populate columns and rows in a table within a
dataset.

1. Create a new and empty dataset then deserialise the XML into that
DataSet, like this:

Dim TempDataSet As New DataSet
Dim MyDeserialiser As XmlSerializer = New _
XmlSerializer(GetType(DataSet))
Dim myFileStream As FileStream

myFileStream = New FileStream("xmlobject.xml", FileMode.Open)
TempDataSet = CType(MyDeserialiser.Deserialize(myFileStream), DataSet)
myFileStream.Close()

If you want to copy the data from the Table in the TempDataSet into a
corresponding Table in some final destination DataSet, you do it like like
this:

For Each Temp_Row As DataRow In TempDataSet.Tables(0).Rows
Dim IncomingYRow As DataRow = Table_Y.NewRow
For Each col As DataColumn In Temp_Row.Table.Columns
IncomingYRow.Item(col.Ordinal) = Temp_Row.Item(col.Ordinal)
Next
Table_Y.Rows.Add(IncomingYRow)
Next

Where Table_Y refers to a table in the destination dataset.

If you deserialise the XML directly to the final destination DataSet without
going through an intermediary temporary dataset then you can expect
problems.

To test the above, serialise an existing dataset in the following manner:

Dim MySerialiser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim MyWriter As StreamWriter

MyWriter = New StreamWriter("xmlobject.xml")
MySerialiser.Serialize(MyWriter, TheDataSet)
MyWriter.Close()

Where TheDataSet is, funny enough, the DataSet.
 
Back
Top