日期:2014-05-20  浏览次数:20993 次

C#中使用XslCompiledTransform类实现将xsl和xml转换为html页面?
问题是这样的,最近要改一个程序功能,需要以前使用这种方式:
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
 xml.Load("xml.txt");
 System.Xml.XmlDocument xsl=new System.Xml.XmlDocument();
 xsl.Load("xsl.txt");
System.IO.StringWriter output = new System.IO.StringWriter();


 System.Xml.Xsl.XslTransform transform = new System.Xml.Xsl.XslTransform();
  transform.Load(xsl.CreateNavigator());
 transform.Transform(xml.CreateNavigator(), null, output);
生成的html改为使用这种方式:
System.Xml.Xsl.XslCompiledTransform transform1 = new System.Xml.Xsl.XslCompiledTransform();
System.Xml.Xsl.XsltSettings set = new System.Xml.Xsl.XsltSettings(false, true);
System.Xml.XmlUrlResolver res = new System.Xml.XmlUrlResolver();
transform1.Load(xsl.CreateNavigator(), set, null);
transform1.Transform(xml.CreateNavigator(), null, output);
但是不知怎么搞的,使用XslCompiledTransform 转化就会报错,错误如下:
不支持包含 Clr 类型“ConcatString”的扩展函数参数或返回值。
   在 System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltResult(XmlQueryType xmlType, Object value)
   在 System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltResult(Int32 indexType, Object value)
   在 <xsl:template name="renderPriceInfo">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position)
   在 <xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position)
   在 Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
   在 Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
   在 System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
   在 System.Xml.Xsl在 System.Xml.Xsl.XslTransformException 中第一次偶然出现的“System.Data.SqlXml.dll”类型的异常
.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
   在 System.Xml.Xsl.XmlILCommand.Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
   在 System.Xml.Xsl.XslCompiledTransform.Transform(String inputUri, XsltArgumentList arguments, TextWriter results)
   在 WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\vs_workspace\WindowsFormsApplication2\WindowsFormsApplication1\Form1.cs:行号 43

异常中说报“ConcatString”这个错误,但我在xsl中并没有找到对应的调用方法,请问我该如何处理,xsl和xml的内容太长这里就没有贴出来,如有需要我单独将文件发给大神,小弟没多少分,麻烦各位大神帮忙解答,先谢了!!



------解决方案--------------------
看看是不是这个问题。

http://www.cnblogs.com/conis/archive/2009/10/18/1720466.html

节选:

最后的排错结果居然是+=上,把result += getStaticFile(domain, file[i], type) + "\n";改为result = result + getStaticFile(domain, file[i], type) ;,问题解决