[英]Class with Collection Properties are not binding properly when posted as XML to ASP.Net Core 3.1 Web API
I'm trying to post my data as XML
to my asp.net core 3.1 web api.我正在尝试将我的数据作为
XML
到我的 asp.net core 3.1 web api。 However Collection properties are not getting binded in my model.但是 Collection 属性没有绑定到我的模型中。
Here is my class,这是我的课,
public class Test
{
public int Usrno { get; set; }
public string PCname { get; set; }
public List<Best> Best { get; set; }
}
public class Best
{
public string Hello { get; set; }
public Worst[] Worst { get; set; }
}
public class Worst
{
public int Ko { get; set; }
public Win[] Win { get; set; }
}
public class Win
{
public string Kiss { get; set; }
}
Here is my POST
end point,这是我的
POST
终点,
[HttpPost]
[Consumes("application/xml")]
[Produces("application/xml")]
public IActionResult Create([FromBody]Test data)
{
return Created("", data);
}
Here is my XML
input,这是我的
XML
输入,
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<Usrno>0</Usrno>
<PCname>string</PCname>
<Best>
<Hello>string</Hello>
<Worst>
<Ko>0</Ko>
<Win>
<Kiss>string</Kiss>
</Win>
</Worst>
</Best>
</Test>
Here is the screen print of the POST
method in API,这是 API 中
POST
方法的屏幕打印,
Here is my ConfigureServices
in Startup.cs
,这是我在
Startup.cs
ConfigureServices
,
services
.AddControllers()
.AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; })
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
I couldn't figure out what I'm missing.我无法弄清楚我错过了什么。 Please assist
请协助
Try using a XmlElementAttribute
on Best
element.尝试在
Best
元素上使用XmlElementAttribute
。
XmlElement attribute indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it. XmlElement 属性指示公共字段或属性在 XmlSerializer 序列化或反序列化包含它的对象时表示 XML 元素。
C# C#
public class Test
{
public int Usrno { get; set; }
public string PCname { get; set; }
[XmlElement("Best")]
public List<Best> Best { get; set; }
}
The MVC's XmlSerializerInputFormatter
calls XmlSerializer
to deserialize the body of the request and the formetter uses this attribute to mark XML elements. MVC 的
XmlSerializerInputFormatter
调用XmlSerializer
来反序列化请求的主体,并且格式化程序使用此属性来标记 XML 元素。
An array or List in xml serialization expects two tags like "Names" and "Name". xml 序列化中的数组或列表需要两个标签,如“名称”和“名称”。 You only have one tag so you need to add the attribute XmlElement.
您只有一个标签,因此您需要添加属性 XmlElement。 This issue occurs in multiple places in your classes.
此问题发生在您班级的多个位置。 I fixed all the issue.
我解决了所有问题。 See classes below
请参阅下面的课程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Test));
Test test = (Test)serializer.Deserialize(reader);
}
}
public class Test
{
public int Usrno { get; set; }
public string PCname { get; set; }
[XmlElement("Best")]
public List<Best> Best { get; set; }
}
public class Best
{
public string Hello { get; set; }
[XmlElement("Worst")]
public Worst[] Worst { get; set; }
}
public class Worst
{
public int Ko { get; set; }
[XmlElement("Win")]
public Win[] Win { get; set; }
}
public class Win
{
public string Kiss { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.