How to return XML to the Response stream?

DaveDev picture DaveDev · Nov 10, 2010 · Viewed 11.3k times · Source

I'm trying to return an xml string from a IHttpHandler to a like this:

context.Response.Write(xml);

When I receive the response in my .aspx.cs I try to load the document as follows:

var xml = XDocument.Load(xmlString);

but I get an Illegal Characters in Path error message.

I've also tried

context.Response.Write(context.Server.HtmlEncode(xml));

and

var xml = XDocument.Load(Server.HtmlDecode(xmlString));

but I get the same message. Is there any way I can return XML from my IHttpHandler?

Answer

BrokenGlass picture BrokenGlass · Nov 10, 2010

replace this:

var xml = XDocument.Load(xmlString);

with this:

var xml = XDocument.Parse(xmlString);