1

When I call my webservice, it only returns the string "System.Xml.XmlDocument" rather than the actual XML. What do I need to change to get it to return an actual XML document?

 public XmlDocument GetCommoditiesXmlDocument() {
            XmlDocument xdoc = new XmlDocument();
            StringWriter sw = new StringWriter();
            XmlTextWriter xtw = new XmlTextWriter(sw);

            //gets XML as XmlElement

            quotes.WriteTo(xtw);
            xdoc.LoadXml(sw.ToString());
            return xdoc;
        }

I'm using .NET 4.0 (and MVC3 if it matters)

1 Answer 1

2

ASP.Net MVC does not know how to serialize an XmlDocument into an HTTP response.

Instead, you should return the XML source directly:

return Content(sw.ToString(), "text/xml");
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure I understand. I actually get the XML passed to my service as an XmlElement. If there a way to serialize that into an HTTP response?
+1 It looks like the Content class is intended to be used with master pages in Web Forms (though I may be wrong), but you did set me on the right track. I ended up using 'Response.ContentType = "text/xml";' Thanks a bunch :)
@Llepwryd: Use the Content() method in the controller.

Your Answer

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