tags:

views:

177

answers:

3

I need to output XML / ASX on an ASPX page. The XML is generated from the code behind and will look like this.

I'm using string builder to create the XML / ASX.

            (...)
            sb.AppendLine("<asx version='3.0'>");
            sb.AppendLine("<title> Spilliste </title>");
            while (i < pdc.Count)
            {
                sb.AppendLine("<entry>");
                sb.AppendLine("<title>" + pdc[i].PageName + "</title>");
                sb.AppendLine("<abstract> Ikke tilgjengelig</abstract>");
                sb.AppendLine("<ref>" + pdc[i].LinkURL + "</ref>");
                sb.AppendLine("</entry>");
                i++;
            }
            sb.AppendLine("</asx>");

            return sb.ToString();
            (...)

But how can I output this?

Response.Write does not work from code behind. And I'm notable to use <asp:label> in the ASPX file, because it needs to be placed within tags. I basically have a blank ASPX page.

What to do?

+2  A: 

Don't use a Page for this. Basically Pages are for rendering html. If you want to send xml or images or any other type of data for that matter you should use a .ashx file and and a class that implements IHttpHandler.

You can see this example on how to implement the interface.

klausbyskov
Oooh, thanks :)
Steven
A: 

Response.Write should work from code behind:

  Response.Write("some test");
  Response.Flush();

But you should execute this code in Page_Load method. You should prepare separate aspx page for generating xml and redirect user that page. This page should be empty (only <%@ ... %> in aspx file).

PanJanek
A: 

You should use an IHttpHandler for this - you can configure which URL's it handles either by making an .ashx file (very simple) or by registering them in the web.config file, which is more flexible, but trickier since the syntax various between classic and integrated mode IIS.

Then, you'll need a simple class with one important member - a ProcessRequest method taking only one parameter - the HttpContext.

Further, avoid using a StringBuilder to build XML. You can use the safer and more flexible linq to xml classes instead: Using this type-safe approach also makes it way easier to write helper methods to generate parts of the xml tree correctly; and you can query and transform the results to boot.

void ProcessRequest(HttpContext context) {
    var pdc = Enumerable.Range(0,10).Select(
        i=>new{PageName="Page"+i,LinkURL="Link"+i});                

    var xmlString = 
    new XElement("asx",
        new XAttribute("version","3.0"),
        new XElement("title","Spilliste"),
        pdc.Select(pdcElem=>
            new XElement("entry",
                new XElement("title",pdcElem.PageName),
                new XElement("abstract","Ikke tilgjengelig"),
                new XElement("ref",pdcElem.LinkURL)
            )
        )
    ).ToString(SaveOptions.DisableFormatting);

    //don't forget to handle headers and set things like content-type too!
    context.Response.Write(xmlString);
}

The reason this approach is better than hacking around a .aspx file is that although it's possible in a .aspx file, you'll need to fight the framework to get there - a whole bunch of infrastructure will get in your way, perhaps subtly mangling your output before it reaches the client. The easiest way to get rid of that is by using a more bare-to-the-metal handler that isn't specifically intended to host aspx controls and generate HTML.

Eamon Nerbonne