【问题标题】:Remove ArrayOf From asp.net 【core】Web Api xml responseRemove ArrayOf From asp.net 【核心】Web Api xml响应
【发布时间】:2020-09-19 00:29:58
【问题描述】:

我使用 Visualstudio 2019 创建默认的 Asp.net 核心 Web API 解决方案。

更改 services.AddControllers();到 services.AddControllers().AddXmlSerializerFormatters();

将获取请求发送至https://localhost:xxxxx/weatherforecast。 我得到以下回应

<ArrayOfWeatherForecast xmlns:xsi="http://www.w3.org.....>
  <WeatherForecast>
  ...
  </WeatherForecast>
......
</ArrayOfWeatherForecast>

但我真的很想得到这样的回应

<WeatherForecasts>
  <WeatherForecast>
  ...
  </WeatherForecast>
......
</WeatherForecasts>

我找到了一些关于从 asp.net 中删除 arrayof 的答案。 请帮我。如何在 asp.net core 中做到这一点。

【问题讨论】:

  • 我认为这里有一些示例代码可以提供帮助。

标签: c# xml asp.net-core-webapi


【解决方案1】:

您是否查看过 Microsoft 上的文档:https://docs.microsoft.com/en-us/dotnet/standard/serialization/controlling-xml-serialization-using-attributes

public class Group {
    [XmlArray("TeamMembers")]
    public Employee[] Employees;
}

属性在序列化中给出名字是这样的

<Group>
<TeamMembers>
    <Employee>
        <Name>Haley</Name>
    </Employee>
</TeamMembers>
</Group>

【讨论】:

  • 嗨,Athanasios Kataras,谢谢。使用你的方法我删除了arrayof。并使用此post 删除了 xml 中的命名空间。现在我得到了我想要的格式。谢谢你非常匹配。
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 2018-08-27
  • 2022-11-04
  • 1970-01-01
  • 2020-01-24
  • 2017-01-30
  • 1970-01-01
相关资源
最近更新 更多