UltraJ
UltraJ

Reputation: 487

Display an ASP.NET as XML

How do you get an ASP.NET page to display XML only instead of HTML? I would like a page to display XML in a similar way as this site: http://weather.yahooapis.com/forecastrss?w=2502265. If you view the source, its XML.

The code that I have writes XML to a Literal on an ASP.NET page.

The ASP.NET page:

<body>
  <form id="form1" runat="server">
    <div>
      <asp:Literal ID="xmlOutput" runat="server" />
    </div>
  </form>
</body>

The C# code:

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string xml = Server.HtmlEncode 
                 (
                   "<?xml version=\"1.0\" encoding=\"utf-8\" ?><br />"
                 + "<Mall>"
                 + "  <Stores>"
                 + "    <Store id=\"\">"
                 + "      <LandingDomain></LandingDomain>"
                 + "      <LandingFolder></LandingFolder>"
                 + "      <StatusID></StatusID>"
                 + "    </Store>"
                 + "  </Stores>"
                 + "</Mall>"
                 );

    xmlOutput.Text = xml;
  } // protected void Page_Load(object sender, EventArgs e)
} // public partial class _Default : System.Web.UI.Page

Upvotes: 1

Views: 160

Answers (1)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56202

You can create HTTP response with desired XML and set Content-Type:text/xml. In this case browser opens this document in default XML viewer or displays document in a proper way.

Upvotes: 2

Related Questions