繁体   English   中英

具有集合属性的类在作为 XML 发布到 ASP.Net Core 3.1 Web API 时未正确绑定

[英]Class with Collection Properties are not binding properly when posted as XML to ASP.Net Core 3.1 Web API

我正在尝试将我的数据作为XML到我的 asp.net core 3.1 web api。 但是 Collection 属性没有绑定到我的模型中。

这是我的课,

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

这是我的POST终点,

[HttpPost]
[Consumes("application/xml")]
[Produces("application/xml")]
public IActionResult Create([FromBody]Test data)
{
    return Created("", data);
}

这是我的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>

这是 API 中POST方法的屏幕打印,

在此处输入图片说明

这是我在Startup.cs ConfigureServices

services
    .AddControllers()
    .AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; })
    .AddXmlSerializerFormatters()
    .AddXmlDataContractSerializerFormatters();

我无法弄清楚我错过了什么。 请协助

尝试在Best元素上使用XmlElementAttribute

XmlElement 属性指示公共字段或属性在 XmlSerializer 序列化或反序列化包含它的对象时表示 XML 元素。

C#

public class Test
{
    public int Usrno { get; set; }
    public string PCname { get; set; }

    [XmlElement("Best")]
    public List<Best> Best { get; set; }
}

MVC 的XmlSerializerInputFormatter调用XmlSerializer来反序列化请求的主体,并且格式化程序使用此属性来标记 XML 元素。

xml 序列化中的数组或列表需要两个标签,如“名称”和“名称”。 您只有一个标签,因此您需要添加属性 XmlElement。 此问题发生在您班级的多个位置。 我解决了所有问题。 请参阅下面的课程

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM