Please take a look at the following code, as I get the error at this line:
xslt.Transform(mydoc.CreateReader(), writer);
Error:
Step into: Stepping over non-user code 'System.Xml.Linq.XNode.CreateReader' A first chance exception of type 'System.NullReferenceException' occurred in System.Data.SqlXml.dll
((System.NullReferenceException)(ex))
PromotionsDataContext db = new PromotionsDataContext();
//XmlDocument myxml;
XElement Categories =
new XElement("Promotions",
from b in db.GetPromotions()
select new XElement("Promotion",
new XElement("Category", b.CategoryName),
new XElement("Client", b.ClientName),
new XElement("Title", b.Title)));
XDocument mydoc = new XDocument();
mydoc.Add(Categories);
try
{
XDocument newTree = new XDocument();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;
//using (XmlWriter writer = newTree.CreateWriter())
using (XmlWriter writer = XmlWriter.Create(newTree.CreateWriter(), settings))
{
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new FileStream(@"C:\1\TransList.xslt", System.IO.FileMode.Open)));
// Execute the transform and output the results to a writer.
xslt.Transform(mydoc.CreateReader(), writer);
}
Console.WriteLine(newTree);
}
catch (Exception ex)
{
Console.Write(ex);
}
Here is the XSLT:
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' />
<xsl:key name='categories' match='Category' use='.' />
<xsl:template match='/'>
<xsl:for-each select="/Promotions/Promotion/Category[
generate-id(.) = generate-id(key('categories', .)[1])
]">
<xsl:variable name='cname' select='.' />
<Category title='{.}'>
<xsl:for-each select='/Promotions/Promotion[Category=$cname]'>
<Title>
<xsl:value-of select='Title' />
</Title>
</xsl:for-each>
</Category>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The error:
System.NullReferenceException: Object reference not set to an instance of an object. at System.Xml.Xsl.Runtime.XmlMergeSequenceWriter.StartTree(XPathNodeType rootType, IXmlNamespaceResolver nsResolver, XmlNameTable nameTable) at System.Xml.Xsl.Runtime.XmlQueryOutput.StartTree(XPathNodeType rootType) at System.Xml.Xsl.Runtime.XmlQueryOutput.WriteStartRoot() at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter) at System.Xml.Xsl.XmlILCommand.Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results) at Promo.Page_Load(Object sender, EventArgs e) in c:\1\promo.ascx.cs:line 144
Now if I do this it works:
StringWriter sw = new StringWriter();
xslt.Transform(mydoc.CreateReader(),null, sw);
What am I doing wrong with the XmlWriter?
Value of xdoc right before crash:
<Promotions>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client1</Client>
<Title>Get your Free 2</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client1</Client>
<Title>Get your Free 4</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>client1</Client>
<Title>Get your Free 5</Title>
</Promotion>
<Promotion>
<Category>Community & Neighborhood</Category>
<Client>Client2</Client>
<Title>Get your Free 1</Title>
</Promotion>
<Promotion>
<Category>Education</Category>
<Client>Client3</Client>
<Title>Get Your Free 3</Title>
</Promotion>
</Promotions>