0
我試圖轉換使用XML:在特殊字符的XSL上使用XSLTCompiledTransform?
private string transformXML(string xml_data, string xml_style)
{
//generemos un html temporal para mostrar la transformacion
string outfile = Path.GetTempFileName().Replace(".tmp", ".html");
XslCompiledTransform proc = new XslCompiledTransform();
using (System.IO.StringReader sr = new System.IO.StringReader(xml_style))
{
using (XmlReader xr = XmlReader.Create(sr))
{
proc.Load(xr);
}
}
string resultXML;
using (System.IO.StringReader sr = new System.IO.StringReader(xml_data))
{
using (XmlReader xr = XmlReader.Create(sr))
{
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
proc.Transform(xr, null, sw);
resultXML = sw.ToString();
System.IO.File.WriteAllText(outfile, resultXML);
return outfile;
}
}
}
}
利用這一點,我上線porc.Load(xr)
得到一個System.Xml.Xsl.XslLoadException
,由於非法'<'
。非法字符在該行的<
:
<xsl:when test="$value < 19">
是,如你所見,逃過一劫。可能,Xml.Create()正在回顧我的<,這是怎麼回事?
它從臨時文件加載,該文件具有正確的'<'字符。我使用System.IO.File.ReadAllText(outfile)將它加載到一個字符串上,你認爲這可能是它? – jlasarte
我不這麼認爲:因爲System.IO.File.ReadAllText()是我在上面的例子中使用的,並且工作正常。我將逐個構建XSL文件,直到它調用proc.Load(xr)爲止。 – Aidanapword
是的,我注意到添加評論後。那麼,我會嘗試你的建議。 – jlasarte