0

I'm trying to add an XSL file reference to an existing code that generates an XML sitemap on the fly. There is no actual physical XML file as the code creates an XML response through a generic handler that responds to a url rewrite path (the handler is called SitemapProvider.ashx and handles requests to sitemap.xml). My end goal is to eventually style and decorate the boring sitemap.xml.

Here's a snippet of my code:

        public void ProcessRequest(HttpContext context)
    {

        XmlDocument doc = ShowXML(context);
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;

        context.Response.Expires = -1;
        context.Response.Cache.SetAllowResponseInBrowserHistory(true);

        doc.Save(context.Response.Output);
    }

    private XmlDocument ShowXML(HttpContext context)
    {
        XmlDocument doc = new XmlDocument();
        var useXSL = doc.CreateProcessingInstruction(
            "xml-stylesheet", "type=\"text/xsl\" href=\"~/sitemap.xsl\""
            );
        doc.AppendChild(useXSL);
        doc.LoadXml(makeXML());

        return doc;
    }

    private string makeXML()
    {
        string xmlString = "";

        xmlString +=
            //"<?xml-stylesheet type=\"text/xsl\" href=\"/sitemap.xsl\"?>" +
            "<urlset xmlns = \"http://www.sitemaps.org/schemas/sitemap/0.9\">" +
            // and so it goes

I can't seem to get either the code in ShowXML() to work or the one I commented out in makeXML().

What am I doing wrong?

2
  • Is there a reason why you need to use an XmlDocument? Wouldn't it be easier to just generate a string with the XML you want and write it to the response directly? Commented Sep 7, 2017 at 1:56
  • Mostly for consistency and validation. I'm still learning C#, this is only my 10th week with the language. If I miss out things that would cause the xml to be invalid, the thing would throw an exception and not output, so that helps my learning process. Commented Sep 7, 2017 at 16:23

1 Answer 1

1

With the order of statements

   doc.AppendChild(useXSL);
    doc.LoadXml(makeXML());

the LoadXml would erase any nodes in the document (like the processing instruction inserted earlier) so that approach can't work. If you want to construct the XML using string concatenation then you would ned to use that first and then PrependChild the processing instruction, I would rather suggest to use a single XML specific API like the XMLDocument Create... factory methods consistently or to switch to XmlWriter or these days to LINQ to XML to construct the XML.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. I swapped the order and used PrependChild and it works as expected. As in it loaded the XSL file. Now I gotta work on that file because it's just loading my title template and not parsing any links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.