Groups | Blog | Home
all groups > c# > march 2006 >

c# : How properly return XML from an ASPX page?



Michael Bray
3/17/2006 8:43:19 PM
"Ronald S. Cook" <rcook@westinis.com> wrote in
news:#PdEKUkSGHA.4920@tk2msftngp13.phx.gbl:

[quoted text, click to view]

Specify the Response.ContentType:

Response.ContentType = "text/xml";

Also, take out ALL other tags, including <html> and <body>. You should
only be left with the <%@ .... %> and <% ... %> tags.

Ronald S. Cook
3/17/2006 9:32:50 PM
I have an ASPX page that returns XML to the calling client. While the code
below works, I don't think it's proper because it's not encoded as XML.
I.e. special characters might screw things up.



Can anyone please tell me the more proper way to do this?



<%@ Page Language="C#" %>



<html>

<body>

<%

Response.Write("<?xml version='1.0'?>");

Response.Write("<Westin><Event><Name>RECEPTION</Name><Floor>2nd
Floor</Floor><StartTime>8:00 AM</StartTime><EndTime>10:00
AM</EndTime></Event></Westin>");

%>



</body>

</html>



Thanks,

Ron



Michael Bray
3/17/2006 9:55:49 PM
"Ronald S. Cook" <rcook@westinis.com> wrote in
news:O5HCOxkSGHA.4492@TK2MSFTNGP09.phx.gbl:

[quoted text, click to view]

Replace the '&' characters with '&amp;' (make sure you include the semi-
colon.)

Michael Bray
3/17/2006 10:14:43 PM
"Ronald S. Cook" <rcook@westinis.com> wrote in
news:#S7C6IlSGHA.4920@tk2msftngp13.phx.gbl:

[quoted text, click to view]

You would have to process each value as you go... Alternatively, you
could create an XmlDocument class and populate it with XMLNodes. Then use
an XmlTextWriter and XmlDocument.WriteTo(...) to write out the final
contents... I believe that it should correctly format whatever 'wierd'
characters may be inside your data.

Ronald S. Cook
3/17/2006 10:24:53 PM
Michael,

I put the Response.ContentType = "text/xml"; line just above my
Response.Write(strXML); line, but since my source text has ampersands in it,
I still get

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and
then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
Whitespace is not allowed at this location. Error processing resource
'http://localhost/delphi/westin/westin.aspx'. Line 1...
<?xml version="1.0"?><Westin><Event><Group>ARK GROUP USA</Group><Booking>1-K
GROUP USA3&...

If I take out the ampersands, it displays fine. I must be missing
something.

Thanks,
Ron



[quoted text, click to view]

Ronald S. Cook
3/17/2006 11:04:43 PM
I did that but get:


Invalid at the top level of the document. Error processing resource
'http://localhost/Westin/Westin.aspx'. Line 1, Positio...

&lt;?xml version="1.0"?&gt;&lt;Westin&gt;&lt;Event&gt;&lt;Group&gt;ARK GROUP
USA &lt;&am...

If I take out the Response.ContentType = "text/xml"; libe then it works, but
it isn't returning true XML, just a string that looks like XML.

I appreciate your help.


[quoted text, click to view]

Ronald S. Cook
3/17/2006 11:07:14 PM
I take that back. That part works ok, but then there are the <> tags. If
there is a stray < tag, how do I change it, but not the <> tags that are
truly XML tags?

Thanks.


[quoted text, click to view]

AddThis Social Bookmark Button