Return XML as HTTP response

I was given a simple task.

When requesting a given URL response, the response should simply be valid XML.

How do I achieve this?

The URL will contain all the necessary code to get the data and build the correct XML string. How can you continue to work and process the response to return only this line? The caller receives the XML string and populates it with the database, so I just have to provide this part of the project.

thanks

+4
source share
5 answers

I would go for the HttpHandler. This way you bypass all asp.net controls, etc. Which is better for performance and since you will not output any html, it makes no sense to use the actual aspx page.

+2
source

Take a look at this:

Response.Clear(); Response.Write(yourXml); Response.ContentType = "text/xml"; Response.End(); 
+9
source

Assuming you have created an XML string, you can clear the answer and just write your own string.

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

If you do not want to use a fully functional web service, you can 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 something similar here using images http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=325

+1
source

Below is a 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(); 
0
source

All Articles