XslCompiledTransform.Load引发未将对象引用设置为对象的实例

时间:2014-09-30 10:18:39

标签: c# .net xml c#-4.0 xslcompiledtransform

在XslCompiledTransform中加载xslt时,我没有将Object引用设置为对象的实例。

我从数据库中获取了以下xslt: -

<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<xsl:template match=\"/\">
   <html>
      <body>
        <h2>My CD Collection</h2>
        <table border=\"1\">
            <tr bgcolor=\"#9acd32\">
               <th style=\"text-align:left\">Title</th>
               <th style=\"text-align:left\">Artist</th>
             </tr>
            <xsl:for-each select=\"catalog/cd\">
             <tr>
               <td>
                    <xsl:value-of select=\"title\"/>
               </td>
               <td>
                    <xsl:value-of select=\"artist\"/>
               </td>
             </tr>
           </xsl:for-each>
        </table>
      </body>
   </html>
</xsl:template>
</xsl:stylesheet>

并跟随数据库中的xml: -

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <cd>
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
</catalog>

使用以下代码转换xslt时: -

public static string XsltTransform(string xmlContent, string xsltContent)
{
    string result = string.Empty;
    XmlTextReader xtr = new XmlTextReader(new StringReader(xsltContent));
    xsltCompiled.Load(xtr);

    using (StringReader srXml = new StringReader(xmlContent))
    {
        using (XmlReader xrXml = XmlReader.Create(srXml))
        {
            using (StringWriter sw = new StringWriter())
            {

                xsltCompiled.Transform(xrXml, null, sw);

                result = sw.ToString();
            }
        }
    }
    return result;
}

但它会在行中引发错误“Object reference not set to an instance of an object”: -

xsltCompiled.Load(xtr);

有什么建议吗?

更新: 只是为了添加更多我在创建XmlTextReader之后得到这个: -

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须删除转义字符。

请检查:Remove the escape sequence '\' from string to convert it to XmlDocument

XsltTransform(xmlContent, xsltContent.Replace("\\\"", "\""));

编辑:( xml,xslt从文件中读取)

    static void Main(string[] args)
    {
        string xmlContent = File.ReadAllText("D:/test/data.xml");
        string xsltContent = File.ReadAllText("D:/test/style.xslt");

        string result = XsltTransform(xmlContent, xsltContent.Replace("\\\"", "\""));
    }

    static readonly XslCompiledTransform XsltCompiled = new XslCompiledTransform();

    public static string XsltTransform(string xmlContent, string xsltContent)
    {
        string result = string.Empty;
        XmlTextReader xtr = new XmlTextReader(new StringReader(xsltContent));
        XsltCompiled.Load(xtr);

        using (StringReader srXml = new StringReader(xmlContent))
        {
            using (XmlReader xrXml = XmlReader.Create(srXml))
            {
                using (StringWriter sw = new StringWriter())
                {

                    XsltCompiled.Transform(xrXml, null, sw);

                    result = sw.ToString();
                }
            }
        }
        return result;
    }