[英]Transform xml using xsl in c# displays empty file
我正在嘗試使用xsl文件轉換xml文件,並在新窗口中打開轉換后的xml文件。 我從SQL Server數據庫中以字符串形式獲取xml和xsl文件,將它們轉換為xml文檔,然后使用xsl轉換xml文件,以下是我的代碼
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);
我的output.xml顯示一個空文件。 我正在發布xsl文件的前幾行代碼,可能太長了,
<?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>
...
任何幫助是極大的贊賞。
它顯示了一個空文件,通過將XML文檔流式傳輸到內存然后從內存中讀取來解決了該錯誤。 代碼如下
MemoryStream stm = new MemoryStream();
xslt.Transform(docXML, null, stm);
stm.Position = 1;
StreamReader sr = new StreamReader(stm);
Response.Write(sr.ReadToEnd());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.