XML from Dataset

  • Thread starter Thread starter Victor Rodriguez
  • Start date Start date
V

Victor Rodriguez

Is there a way to get the XML from Dataset.WriteXML("filename.xml") from a
format:
<Table>
<Field1>
Text1
</Field1>
</Table>

to:
<Table Field1="Text1"></Table>

thanks in advance,

Victor
 
Hi,

First you need to create XSL stylesheet. You could save it into the file and
it would look like (someone probably could provide different stylesheet)
Assuming that dataset name is MyDS and table name is Table 1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="/MyDS">
<xsl:element name="MyNewXML">
<xsl:for-each select="*">
<xsl:element name="Table1">
<xsl:attribute name="Field1"><xsl:value-of
select="Field1"/></xsl:attribute>
<xsl:attribute name="Field2"><xsl:value-of
select="Field2"/></xsl:attribute>
<xsl:attribute name="Field3"><xsl:value-of
select="Field3"/></xsl:attribute>

</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Now, your VB code would look like

Dim loSourceXML as XPath.xPathDocument
Dim loTransform as Xsl.Xsltransform
Dim loXmlOutput as XmlTextWriter

loSourceXML = new XPath.xPathDocument(New StringBuilder(MyDataSet.GetXml()))
loTransform = New Xsl.Xsltransform()
loTransform.Load("Path to the stylesheet")
loXmlOutput = New XmlTextWriter("Path to the output XML file here",Nothing)
loTransform.Transform(loSourceXML,Nothing,loXmlOutput)

Val Mazur
Microsoft MVP
 

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

Back
Top