【问题标题】:System.Xml.XmlException:Root element is missing [closed]System.Xml.XmlException:缺少根元素[关闭]
【发布时间】:2020-10-18 11:40:11
【问题描述】:

我有一个将 json 作为 HttpResponseMessage 返回的 REST Api。 当我在计算机的 localhost 中运行它时,一切正常,我得到了 json 结果。 但是当我将它托管在另一个系统的 IIS 服务器上时,我得到了这个错误:

{
   "Message": "An error has occurred.",

   "ExceptionMessage": "Root element is missing.",

   "ExceptionType": "System.Xml.XmlException",

   "StackTrace": "   at System.Web.Http.ApiController.

<InvokeActionWithExceptionFilters>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
}

我使用 SoapUI 作为 Web 服务测试工具

返回结果代码块:

XmlDocument doc = new XmlDocument();
doc.LoadXml(XML_Result);
string jsonText = JsonConvert.SerializeXmlNode(doc);


response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(jsonText, Encoding.UTF8, "application/json");
// return Request.CreateResponse(HttpStatusCode.OK, response, Configuration.Formatters.JsonFormatter);
return response;

已编辑:调用 LoadXml 时发生错误,其参数来自我在此处调用的另一个服务。 在这行代码中

          doc.LoadXml(XML_Result); 

【问题讨论】:

  • 更好地展示您如何获得XML_Result 值。
  • 嗨@AlexanderPetrov,它来自我在这里称之为的另一个服务
  • 我以为您在调用 doc.LoadXml 方法时会得到显示的错误。是这样吗?
  • 究竟是哪里抛出的错误?

标签: c# asp.net json xml asp.net-mvc


【解决方案1】:

调用 LoadXml 方法时发生错误。 在 doc.LoadXml(XML_Result) XML_Result 返回一个空字符串。 不为空就解决了。

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 2012-04-24
    • 1970-01-01
    • 2016-05-10
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多