DataSet to XML with Schema

saunders_csl

New member
Joined
Apr 30, 2010
Messages
3
Programming Experience
3-5
Hi,

I am currently trying to write a basic web application/page that will give the user XML data based on the results of a query. I have all my code working so that a dataset is created populated and XMl is return to the user, however I would also like the schema included in the return. I have spent some time searching around the different object methods and properties and can not seem to find a soultion that will do this in memory. My cirrent code is below:

Dim myOdbcDataAdapter As Data.Odbc.OdbcDataAdapter = New Data.Odbc.OdbcDataAdapter(SelectCmdString, DSNString)

Dim myDataSet As New Data.DataSet()

myOdbcDataAdapter.Fill(myDataSet, "easyStore_dw")

Dim xmlDoc As System.Xml.XmlDataDocument
Dim xmlDec As System.Xml.XmlDeclaration
Dim xmlWriter As System.Xml.XmlWriter


' setup response
Me.Response.Clear()
Me.Response.ContentType = "text/xml"
Me.Response.Charset = "utf-8"
xmlWriter = New System.Xml.XmlTextWriter(Me.Response.OutputStream, System.Text.Encoding.UTF8)


' create xml data document with xml declaration
xmlDoc = New System.Xml.XmlDataDocument(myDataSet)
xmlDoc.DataSet.EnforceConstraints = False
xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.PrependChild(xmlDec)

' write xml document to response
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Flush()
xmlWriter.Close()
Response.End()

Any help would be much appriciated.

Kind Regards,
 
I still have not managed to find a way to display the schema in the same XML return.

I have found a way to add the schema to the document but not to include it in the feedback:

xmlDoc.DataSet.ReadXmlSchema(New IO.StringReader(myDataSet.GetXmlSchema()))
 
Solved

I have now found a solution:

xmlDoc = New System.Xml.XmlDataDocument
xmlDoc.DataSet.EnforceConstraints = False
xmlDec = xmlDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)
xmlDoc.PrependChild(xmlDec)
myDataSet.WriteXml(xmlWriter, Data.XmlWriteMode.WriteSchema) ' write xml with Schema

xmlDoc.WriteTo(xmlWriter)
xmlWriter.Flush()
xmlWriter.Close()
Response.End()

The only issue I have is that the declaration is appearing at the bottom of my output??

But I am pretty much there now
 
Back
Top