I ran into a real stumper this morning, which the Google helped me fix. Here’s the code in question:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// request.Credentials = new NetworkCredential(user, password);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string xslpath = Server.MapPath("/path/xsl/mytransform.xslt");
XPathDocument xml = new XPathDocument(response.GetResponseStream());
XslCompiledTransform trans = new XslCompiledTransform();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(sw);
this.OutputDiv.InnerHtml = sb.ToString();
This was causing my <div> tags to incorrectly nest themselves in odd ways – a cascade of Russian dolls.
I saw some search results that suggested it was a problem with XslCompiledTransform, and lo and behold when I moved back to the now-deprecated XslTransform class, and turned warnings off, it worked.
Hmm, says I: that can’t be the preferred solution, can it?
The answer is in the third-to-last line above: if you use the XhtmlTextWriter instead of the XmlTextWriter, things work as expected. I have no idea why yet.
XhtmlTextWriter writer = new XhtmlTextWriter(sw);