首页 > 解决方案 > 为什么我必须设置 UseXmlSerializer = true 才能使用 XML 请求?

问题描述

使用 .NET Framework 4.7、C#、Postman。

我发出请求,请求标头 Content-Type 设置为“application/xml”。身体看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<BookOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <books>
    <Book>
      <bookName>Buzz</bookName>
      <code>1234</code>
    </Book>
  </books>
</BookOrder>

我想,开箱即用,.NET Framework 4.7 接受 XML 请求并会以相同的方式响应,还是我弄错了?

相反,我必须在 Global.asax 中设置:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

为什么是这样?我试过不设置它,但无论我如何格式化它都不会接受我的 XML。请问有什么想法吗?

谢谢

我的课程是:

public class BookOrder
{
    public List<Book> books{ get; set; }
}

public class Book
{
    public string bookName { get; set; }
    public string code { get; set; }
}

标签: c#xml

解决方案


推荐阅读