convert datatable to xml in aspx

J

jk

I'm having trouble converting a datatable into xml, with resonse.write
to aspx. I'm basically converting vb code that saved a recordset into a
stream into c#, but the format is wrong. I've tried using streams,
datadoc, xmlreader, etc with no success. I need to convert it directly
to a string or via a stream, but not in a file. Here's some sample
code that gives me the bad format. I'll post the good format below that
give me rowsets rather than tables. thanks for any help!!

StringWriter sw = new StringWriter();
dt.DataSet.WriteXml(sw, XmlWriteMode.WriteSchema);
string s = sw.ToString();
Response.Write("<?xml version='1.0' ?>");
Response.Write(s);

<!-- give me this: /-->
<?xml version='1.0' ?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:attribute name="VAR_VALUE" type="xs:string" />
<xs:attribute name="VAR_KEY" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table VAR_VALUE="LSUS" VAR_KEY="DIVISION" />
<Table VAR_VALUE="DB2T" VAR_KEY="DB2DSN" />
<Table VAR_VALUE="TRUE" VAR_KEY="PRODPROF" />
</NewDataSet>

<!-- I need this: /-->
<?xml version='1.0' ?>
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly'>
<s:AttributeType name='VAR_VALUE' rs:number='1'
rs:nullable='true'
rs:writeunknown='true'>
<s:datatype dt:type='string' rs:dbtype='str'
dt:maxLength='200'/>
</s:AttributeType>
<s:AttributeType name='VAR_KEY' rs:number='2'
rs:writeunknown='true'>
<s:datatype dt:type='string' rs:dbtype='str'
dt:maxLength='10'
rs:maybenull='false'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row VAR_VALUE='LSUS' VAR_KEY='DIVISION'/>
<z:row VAR_VALUE='DB2P' VAR_KEY='DB2DSN'/>
<z:row VAR_VALUE='TRUE' VAR_KEY='PRODPROF'/>
</rs:data>
</xml>
 
J

jk

that gives me this:
<NewDataSet>
<Table>
<VAR_VALUE>LSUS</VAR_VALUE>
<VAR_KEY>DIVISION</VAR_KEY>
</Table>
<Table>
<VAR_VALUE>DB2T</VAR_VALUE>
<VAR_KEY>DB2DSN</VAR_KEY>
</Table>
<Table>
<VAR_VALUE>TRUE</VAR_VALUE>
<VAR_KEY>PRODPROF</VAR_KEY>
</Table>
</NewDataSet>

none of the options i've tried gives me the output expected from
responseStream. it's like it's interpreting the data as a higher node
(maybe since it's dataset not datatable). i need rowset not table info.
this was just a few lines of code in vb.net that worked fine:

stream = Server.CreateObject("ADODB.Stream")
stream.Type = 1
stream.Open()
rs.save(stream, 1) '1=adpersistxml
sXML = stream.ReadText
stream.Close()
stream = Nothing
Response.Write("<?xml version='1.0' ?>")
Response.Write(sXML)
 
S

Scott M.

Going back and reading your first post again, I see that what you say that
you want is data in the MS XDR format (XML Data Reduced). XDR was replaced
by the W3C XSD (XML Schema Document) standard, some time ago.

The XML methods of ADO.NET objects do not produce XML in the format that you
seek (XDR).

Since what you want is produced via the, now legacy, ADO Recordset and since
the code that you says works for you is all Classic ADO/COM code, you are
probably better off, sticking with Classic COM, rather than .NET. Or, you
may want to consider writing a COM component that does the job and then call
that component from within .NET using COM interOp.
 
T

Teemu Keiski

Hi,

see the code download for Dino Esposito's "Applied XML Programming for
Microsoft .NET"

http://www.microsoft.com/MSPress/books/6235.asp .

In chapter 4 there is an example of an XmlRecordsetWriter class (custom XML
writer) which
is capable to write ADO recordset compliant XML. It has WriteRecordset
method which can take a DataSet, DataTable or DataView and write it as
Recordset.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top