I am trying to convert xml file into csv which executes code but data is not pulled from xml to csv.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Here is the method that tranform xml to xsl.Please let me know where its failing?

public void ConverttoCSV_Click(object sender, EventArgs e)<br/>
{<br/>
string xsl = @"C:test.xsl";<br/>
XmlReader rds = XmlReader.Create(@"C:test.xml");<br/>
XPathDocument doc = new XPathDocument(rds);<br/>
<br/>
XslCompiledTransform xslt = new XslCompiledTransform();<br/>
xslt.Load(xsl);<br/>
XmlWriter wrt = XmlWriter.Create(@"C:test.csv");<br/>
xslt.Transform(rds, wrt);<br/>
MessageBox.Show("Success");<br/>
rds.Close();<br/>
wrt.Close();<br/>
}
Here is the .XSL template File:

<?xml version="1.0"?><br/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv <br/>
<xsl:output method="text" encoding="utf-8" /><br/>
<xsl:strip-space elements="*" /><br/>
<br/>
<xsl:variable name="delimiter" select="," /><br/>
<br/>
<csv:columns><br/>
<column>name</column><br/>
<column>sublease</column><br/>
<column>addressBookID</column><br/>
<column>boundAmount</column><br/>
<column>rentalAmount</column><br/>
<column>rentalPeriod</column><br/>
<column>rentalBillingCycle</column><br/>
<column>tenureIncome</column><br/>
<column>tenureBalance</column><br/>
<column>totalIncome</column><br/>
<column>balance</column><br/>
<column>available</column><br/>
</csv:columns><br/>
<br/>
<xsl:template match="/property-manager/properties <br/>
<!-- Output the CSV header --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<xsl:value-of select="."/><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<xsl:text>&#xa;</xsl:text><br/>
<br/>
<!-- Output rows for each matched property --><br/>
<xsl:apply-templates select="property" /><br/>
</xsl:template><br/>
<br/>
<xsl:template match="property <br/>
<xsl:variable name="property" select="." /><br/>
<br/>
<!-- Loop through the columns in order --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<!-- Extract the column name and value --><br/>
<xsl:variable name="column" select="." /><br/>
<xsl:variable name="value" select="$property/*[name() = $column]" /><br/>
<br/>
<!-- Quote the value if required --><br/>
<xsl:choose><br/>
<xsl:when test="contains($value, &quot;) <br/>
<xsl:variable name="x" select="translate($value, &quot;, &quot;&quot;)"/><br/>
<xsl:value-of select="concat(&quot;, $x, &quot;)"/><br/>
</xsl:when><br/>
<xsl:when test="contains($value, $delimiter) <br/>
<xsl:value-of select="concat(&quot;, $value, &quot;)"/><br/>
</xsl:when><br/>
<xsl:otherwise><br/>
<xsl:value-of select="$value"/><br/>
</xsl:otherwise><br/>
</xsl:choose><br/>
<br/>
<!-- Add the delimiter unless we are the last expression --><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<br/>
<!-- Add a newline at the end of the record --><br/>
<xsl:text>&#xa;</xsl:text><br/>
</xsl:template><br/>
<br/>
</xsl:stylesheet>


<span style="white-space:pre t<span style="white-space:pre hanks in advance
<hr class="sig satwick

View the full article
 
Back
Top