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