【问题标题】:Webservice returns only System.Xml.XmlDocument stringWebservice 仅返回 System.Xml.XmlDocument 字符串
【发布时间】:2013-01-20 17:28:00
【问题描述】:

当我调用我的 web 服务时,它只返回字符串“System.Xml.XmlDocument”而不是实际的 XML。我需要改变什么才能让它返回一个实际的 XML 文档?

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

我正在使用 .NET 4.0(如果重要的话,还有 MVC3)

【问题讨论】:

    标签: asp.net asp.net-mvc-3 web-services c#-4.0


    【解决方案1】:

    ASP.Net MVC 不知道如何将XmlDocument 序列化为 HTTP 响应。

    您应该直接返回 XML 源代码:

    return Content(sw.ToString(), "text/xml");
    

    【讨论】:

    • 我不确定我是否理解。我实际上将 XML 作为 XmlElement 传递给我的服务。如果有办法将其序列化为 HTTP 响应?
    • +1 看起来 Content 类旨在与 Web 表单中的母版页一起使用(尽管我可能错了),但您确实让我走上了正确的道路。我最终使用了 'Response.ContentType = "text/xml";'非常感谢:)
    • @Llepwryd:在控制器中使用Content() 方法
    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多