Reputation: 1
I am trying to transform an xml file using an xsl file and open the the transformed xml file in a new window. I am fetching xml and xsl file from sql server database as strings , convert them into xml documents and then transforming the xml file using xsl, Below is my code
IQueryable<DataDictionaryReport> reportsQuery = from d in dataDictionaryContext.DataDictionaryReports
where d.DBID == j
select d;
string displayXML=null ;
string displayXSL=null;
foreach (var report in reportsQuery)
{
displayXML = report.ReportXML;
displayXSL = report.ReportXSL;
}
//myLabel.Text = displayXML;
XmlDocument docXML = new XmlDocument();
docXML.LoadXml(displayXML);
XmlTextWriter writerXML = new XmlTextWriter(Server.MapPath("/document/Model Report.xml"), null);
writerXML.Formatting = Formatting.Indented;
docXML.Save(writerXML);
XmlDocument docXSL= new XmlDocument();
docXSL.LoadXml(displayXSL);
XmlTextWriter writerXSL = new XmlTextWriter(Server.MapPath("/document/Model Report.xsl"), null);
writerXSL.Formatting = Formatting.Indented;
docXSL.Save(writerXSL);
try
{
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("/document/output.xml"), null);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("/document/Model Report.xsl");
xslt.Transform("/document/Model Report.xml", "/document/output.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
string newWin = "window.open('" + "/document/output.xml" + "');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
My output.xml displays an empty file. I am posting the first few lines of code for the xsl file, probably its too long,
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="IDs" match="*" use="@id" />
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Table/Columns Report</title>
<script type="text/javascript">
// <![CDATA[
function toggle(element, togimg) {
if (element.style.display == 'none') {
element.style.display = 'block';
togimg.src="images/collapse.gif";
}
else {
element.style.display = 'none';
togimg.src="images/expand.gif";
}
}
// ]]></script>
</head>
...
Any help is greatly appreciated.
Upvotes: 0
Views: 1323
Reputation: 1
It displayed an empty file, the error was resolved by streaming the XML document to memory and then reading from memory. The code is as below,
MemoryStream stm = new MemoryStream();
xslt.Transform(docXML, null, stm);
stm.Position = 1;
StreamReader sr = new StreamReader(stm);
Response.Write(sr.ReadToEnd());
Upvotes: 0