How can I output XML from code behind in an ASPX file?

眉间皱痕 提交于 2019-12-10 10:35:36

问题


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?


回答1:


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.




回答2:


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).




回答3:


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.




回答4:


    Response.ClearHeaders();
    Response.ContentType = "text/xml;charset=UTF-8";
    string xmlString = "<aaa>sai</aaa>";
    Response.Write(xmlString);
    Response.End();


来源:https://stackoverflow.com/questions/2295892/how-can-i-output-xml-from-code-behind-in-an-aspx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!