Stream DataSet into XMLDocument

  • Thread starter Thread starter Rob Panosh
  • Start date Start date
R

Rob Panosh

Hello,

I am trying to create a xmlDocument from as dataset. My code is listed
below. All seems to go well until
xmlDocument.Load(CType(ms, System.IO.Stream)) ... I keep getting the
following error "The Root Element is Missing" ... any help would be
appreciated.

Thanks,
Rob Panosh

Public Function GetXMLDocument() As System.String
Dim xDataSet As New System.Data.DataSet
Dim xmlDocument As New System.Xml.XmlDocument
Dim ms As New System.IO.MemoryStream ' = Nothing
Dim ser As System.Xml.Serialization.XmlSerializer
Dim t As System.Type

t = GetType(System.Data.DataSet)
ser = New System.Xml.Serialization.XmlSerializer(t) '
System.Type.GetType("System.Data.DataSet"))

'Filled with data adapter.
Me.Execute(xDataSet, "test")

ser.Serialize(ms, xDataSet)
xmlDocument.Load(CType(ms, System.IO.Stream))

End Function
 
The error tells me that what you are trying to load into an XMLDocument is
not well-formed XML.

Have you printed out the data that "ms" contains? Also, you seem to be
going though excessive trouble to take the DataSet data and persist it as
xml when a DataSet has a getXML() method that will do what you want:


Public Function GetXMLDocument() As System.String
Dim xDataSet As New System.Data.DataSet
Dim xmlDocument As New System.Xml.XmlDocument

'....Fill your DataSet here...

'Convert the DataSet data to XML and load it into the
XMLDocument
xmlDocument.Load(xDataSet.GetXML())
End Function
 
Scott,

I am trying to get a xmlDocument that can easily be manipulated at runtime.

Rob
 
Hi Rob,

If you want an simple approach, create your dataset using the designer to
make an XSD, that is the most easy method you can reference to that when you
create your new dataset in your program.

If you want you can also create it in code(not that difficult).

Than you can do a mydataset.writexml(path) and readxml and you have your
dateset.

Know that there are a lot of overloaded functions with datasets and
datatables.

So there is not always one way.

I hope this gives some idea's?

Cor
 
Cor,

Yes that is true .... I was trying to avoid writing a file to disk. I have
it all sorted out.

Thanks for your input.

Rob
 
The code I gave will give you your XMLDocument without the whole trouble of
setting up the stream and serializing.
 
Back
Top