XML output in .aspx page

S

Steve Peterson

Hi

I have an .aspx web form in which I would like to output only XML, no HTML.
The datasource is a datatable that's bult on the fly based on user input
from previous .aspx page so the XML output has to be genereated in the code
behind.

I was wondering if there is an elegant way of doing this in .NET using the
..NET classes...

TIA

Steve
 
L

Lars Netzel

From a DataSet you can call method WriteXML.. Gives a nice XML output! Maybe
you can do that directly from a datatable as well, otherwise just add the
table to a dataset and then call that method.

/Lars Netzel
 
S

Steve Peterson

Hi Lars - thx for the reply.

However, doesn't the WriteXML method write to a file? What I need to do is
make the XML what the cleint recieves when the request an .aspx page. For
example when they request http://www.mydomain.com/xml.aspx the recieve just
XML instead of <html><head></head><body>...</body></html>....

Steve
 
L

Lars Netzel

Well... don't have to write it to a file. Check out the Arguments.. there
are 8 variations.

Try a stream... that shouldn't be to hard to output as a string.

/Lars
 
S

Steve Peterson

Well you are right, Lars - & thx again for the reply

I guess the question & really have is how to get the XML INTO the .aspx page
(without the HTML) and send it back to the client. This is where I'm stuck.

I usually write for windows, but got this ASP.NET project handed to me, so I
don't really know all the tricks of making this stuff work the .aspx way...

Steve
 
L

Lars Netzel

Do you want to show the XML as XML or do you need to parse it some how?

If you only need to show it as xml, you just html-encode what you get from
the XMLWriter and put it on the page!

Unless you have the option to just redirect to the xml fil itself and the
whoel thing will just by it self..

/Lars
 
S

Steve Peterson

Thx for your reply again, Lars

No - I don't want to html-encode it. I just want the page to contain XML
instead of HTML. Il looked at the XML writer & yes - that's what I need.
But, how to I get it into the page? Is there some control I can bind it to
or anything like that?

Steve
 
M

Matt Berther

Hello Steve,

How about:

private void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/xml";

DataSet myDataSet = SomeMethodToGetADataSet();
myDataSet.WriteXml(Response.OutputStream);

Response.Flush();
Response.End();
}
 
B

bruce barker

Response.Clear(); // remove any html in the buffer (or aspx page)
XmlTextWriter xml = new XmlTextWriter(Response.OutputStream,null);

// now use xmlwriter to output xml


-- bruce (sqlwork.com)


| Hi
|
| I have an .aspx web form in which I would like to output only XML, no
HTML.
| The datasource is a datatable that's bult on the fly based on user input
| from previous .aspx page so the XML output has to be genereated in the
code
| behind.
|
| I was wondering if there is an elegant way of doing this in .NET using the
| .NET classes...
|
| TIA
|
| Steve
|
|
 
S

Steve Peterson

thx matt for your reply!

I dound another solution (I can't believe I missed it before) is simply
'mydataset.GetXML'. I put one literal control in the page (I set the
ContentType="text/xml" attribute in the @Page directive & whacked all the
autogenerated HTML), then in code behind its:

...(code to get data set)
mylitcontrol.text = mydataset.GetXML

Viola!

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top