XslCompiledTransform.Load raises Object reference not set to an instance of an object
Asked Answered
C

1

0

While loading xslt in XslCompiledTransform, I am getting Object reference not set to an instance of an object.

I fetched following xslt from 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 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 following code to transform 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 throws error "Object reference not set to an instance of an object" at line :-

xsltCompiled.Load(xtr);

Any suggestions?

Updates: Just to add more I am getting this after create XmlTextReader : -

enter image description here

Cloven answered 30/9, 2014 at 10:18 Comment(0)
C
1

You must remove the escaping characters.

Check this: Remove the escape sequence '\' from string to convert it to XmlDocument

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

edited: (xml, xslt read from files)

    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;
    }
Cutwater answered 30/9, 2014 at 10:36 Comment(1)
Check the edited answer, it works fine. I'm reading content from files by the way, same thing.Cutwater

© 2022 - 2024 — McMap. All rights reserved.