XslCompiledTransform.Load calls a reference to an object that is not set to an instance of the object

When loading xslt in XslCompiledTransform, I get a link to an object not installed in the object instance.

I pulled the following xslt from the database: -

<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>

and following xml from the database: -

<?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>

When using the following code to convert 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;
}

But it gives an error " Object reference not set to an instance of an object" in the line: -

xsltCompiled.Load(xtr);

Any suggestions?

Update: Just add more, I get this after creating the XmlTextReader: -

enter image description here

0
source share
1 answer

You must remove the escape characters.

: escape- '\' , 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;
    }
+1

All Articles