简体   繁体   English

如何从ASPX文件中的代码后面输出XML?

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

I need to output XML / ASX on an ASPX page. 我需要在ASPX页面上输出XML / ASX。 The XML is generated from the code behind and will look like this . XML是从后面的代码生成的, 看起来像这样

I'm using string builder to create the XML / ASX. 我正在使用字符串生成器来创建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. Response.Write在后面的代码中不起作用。 And I'm notable to use <asp:label> in the ASPX file, because it needs to be placed within tags. 而且我值得注意的是在ASPX文件中使用<asp:label> ,因为它需要放置在标签内。 I basically have a blank ASPX page. 我基本上有一个空白的ASPX页。

What to do? 该怎么办?

Don't use a Page for this. 不要为此使用Page Basically Page s are for rendering html. 基本上Page是用于呈现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 . 如果您要发送xml或图像或​​任何其他类型的数据,则应使用.ashx文件和实现IHttpHandler的类。

You can see this example on how to implement the interface. 您可以看到有关如何实现该接口的示例

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

Response.Write should work from code behind: Response.Write应该在后面的代码中工作:

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

But you should execute this code in Page_Load method. 但是您应该在Page_Load方法中执行此代码。 You should prepare separate aspx page for generating xml and redirect user that page. 您应该准备单独的aspx页面以生成xml,并重定向该页面的用户。 This page should be empty (only <%@ ... %> in aspx file). 此页面应该为空(aspx文件中仅<%@ ... %> )。

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. 您应该使用IHttpHandler这个-你可以配置它,处理它的URL 或者通过使一个ashx的文件 (很简单),或者通过web.config文件中注册它们 ,这样更灵活,但棘手因为语法经典之间的各种和集成模式IIS。

Then, you'll need a simple class with one important member - a ProcessRequest method taking only one parameter - the HttpContext. 然后,您将需要一个带有一个重要成员的简单类-一个仅接受一个参数的ProcessRequest方法-HttpContext。

Further, avoid using a StringBuilder to build XML. 此外,请避免使用StringBuilder构建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; 您可以使用更安全,更灵活的LINQ to XML类来代替:使用这种类型的安全方式也使得它比较容易的方式来写辅助方法来生成XML树正确的部分; 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. 这种方法比对.aspx文件进行黑客攻击更好的原因是,尽管可以在.aspx文件中使用它,但您仍需要与框架进行斗争-整堆基础结构都会妨碍您的工作,也许会巧妙地解决您的输出到达客户之前。 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. 摆脱这种情况的最简单方法是使用更裸露的金属处理程序,该处理程序并非专门用于承载aspx控件并生成HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM