5

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

5 Answers 5

10

Take a look at this :

Response.Clear();
Response.Write(yourXml);
Response.ContentType = "text/xml";
Response.End();
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

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
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.
Typically my VS does not offer the Http Handler as an item only GenericHandler
That's the one, it should add an ashx file to your project.
2

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);

1 Comment

You should also set the Response.ContentType="text/xml"
1

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

Comments

0

Below is the way to send xml data to the browser as a response.

  StringBuilder xmlBuilder = new StringBuilder();

        xmlBuilder.Append("<Books>");
        xmlBuilder.Append("<Book>");
        xmlBuilder.Append("Maths");
        xmlBuilder.Append("</Book>");
        xmlBuilder.Append("</Books>");

        context.Response.ContentType = "text/xml";

        context.Response.BinaryWrite(Encoding.UTF8.GetBytes(xmlBuilder.ToString()));
        context.Response.End();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.