tags:

views:

203

answers:

4

I've been given a seemingly simple task.

When a given URL is requested the response should simply be some valid XML.

How do I achieve this.

The url will contain all the necessary code behind to get the data and construct the correct XML string. How do you then go ahead and manipulate the response to return this string only. The caller is receiving the XML string and populating a database with it, that's there responsibility I just need to provide this part of the project.

Thanks

+1  A: 

Assuming you have your XML string created you can clear the response and just write your string out.

Response.Clear();
Response.ContentType = "text/xml";
Response.Write(myXMLString);

Edit: As mentioned, you should also set the content type

Geoff
You should also set the `Response.ContentType="text/xml"`
pjp
+1  A: 

If you don't want to use full blown webservice then you could do something like this:

private void Page_Load(object sender, System.EventArgs e)
{

    Response.ContentType = "text/xml";

    //get data from somewhere...
    Response.Write(data);

  }
}

See here for something similar using images http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=325

pjp
+4  A: 

Take a look at this :

    Response.Clear();
    Response.Write(yourXml);
    Response.ContentType = "text/xml";
    Response.End();
Canavar
+2  A: 

I would go for an HttpHandler. This way you circumvent all asp.net control creation etc. which is better for performance and seeing as you will not be outputting any html there's no point in using an actual aspx page.

Colin
oo very nice but I wouldn't know how to go about this, I also need to query a DB and loop the results etc so I think i'll stick to a page with some code behind creating my XML and then clearing the response as above. Thanks though +1
Robert
It's very simple really, just go to add new item in visual studio and select Http Handler, it will create the codebehind + ashx file for you, then you can code your data retrieval jsut as you would for a page. you can send querystring variables to httphandlers as well, and even use sessions if needed. then using Response.Write(yourXml); Response.ContentType = "text/xml"; you output the xml.
Colin
Typically my VS does not offer the Http Handler as an item only GenericHandler
Robert
That's the one, it should add an ashx file to your project.
Colin
plus codebehind of course
Colin
Works a treat. Thanks very much
Robert