How properly return XML from an ASPX page?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

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

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.

-mdb
 
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
 
If I take out the ampersands, it displays fine. I must be missing
something.

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

-mdb
 
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.
 
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.
 
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?

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.

-mdb
 
Back
Top