简体   繁体   中英

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. However Collection properties are not getting binded in my model.

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,

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

Here is my XML input,

<?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,

在此处输入图片说明

Here is my ConfigureServices in Startup.cs ,

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.

XmlElement attribute indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it.

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.

An array or List in xml serialization expects two tags like "Names" and "Name". You only have one tag so you need to add the attribute 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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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