473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How properly return XML from an ASPX page?

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


Mar 18 '06 #1
6 2241
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:#P**************@tk2msftngp13.phx.gbl:
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
Mar 18 '06 #2
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

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:#P**************@tk2msftngp13.phx.gbl:
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

Mar 18 '06 #3
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:O5**************@TK2MSFTNGP09.phx.gbl:
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
Mar 18 '06 #4
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;G roup&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.
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn**************************@207.46.248.16...
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:O5**************@TK2MSFTNGP09.phx.gbl:
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

Mar 18 '06 #5
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.
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn**************************@207.46.248.16...
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:O5**************@TK2MSFTNGP09.phx.gbl:
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

Mar 18 '06 #6
"Ronald S. Cook" <rc***@westinis.com> wrote in
news:#S**************@tk2msftngp13.phx.gbl:
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
Mar 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Will Waggoner | last post by:
Ok, I've been hung up on this issue for quite some time now and I would like some help in getting it to work. I think this may be a setup issue but I'd like to ask your eminences first. Here's...
1
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to hit Refresh for the images to load properly....
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
5
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
3
by: Darrel | last post by:
I asked this a while ago but got sidetracked and now have finally gotten back to it. Karl wrote a nice (and easy to read) article here: ...
1
by: Darrel | last post by:
APologies for the cross post...posted in the wrong forum the first time. I have an ASPX page on my server that isn't properly running as an ASPX page. At first, the RESX file would load. I delete...
4
by: Charleees | last post by:
GIFS not working properly in JavaScript PopUps Hi all, I have a button and when i click tha button it redirects to another page..... I have also added a java script for the button that...
8
by: Harvey Schmidlapp | last post by:
I have a fairly complex form (generated by means of an ASP 3 page). The form is used to define a query against a database. After running a query, the user hits their browser's back button and goes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.