Gert Dunon
Gert Dunon

Reputation: 71

XML Body is null/default when sending to a web API

I've been banging my head against a wall for the past 2 days about this. For our client we need to make an interface that accepts XML (SOAP) messages in our API (.NET Core 3.1 or .NET 5). I'm at the point where the messages are finally coming in but they're always incomplete/null/default as you can see on the picture.

What is sent by the client (example 1):

enter image description here

What is received in our API:

enter image description here

And if we receive an XML with multiple lists, the lists are null (example 2): enter image description here

enter image description here

What is going on here? Am I missing something?

Edit:

if i arrange the XML alphabetically, it works:

enter image description here enter image description here

And yes in my Startup i used the XmlSerializers: enter image description here

If I use .AddXmlSerializerFormatters() i can't even send the XML.

Upvotes: 3

Views: 967

Answers (1)

Yinqiu
Yinqiu

Reputation: 7190

I tested your code, you can check my step.

TestXML:

public class TestXML
{     
    public int  ID { get; set; }
    public string Name { get; set; }
    public List<TestXML_UoM_Item> TestXML_UoM_List { get; set; }
}

public class TestXML_UoM_Item
{
    public string Name { get; set; }
}

XML:

<TestXML> 
 <TestXML_UoM_List>
    <TestXML_UoM_Item>
      <Name>aa</Name>
    </TestXML_UoM_Item>
    <TestXML_UoM_Item>
      <Name>bb</Name>
    </TestXML_UoM_Item>
 </TestXML_UoM_List>
 <ID>3</ID>
 <Name>Gert</Name>
</TestXML>

StratUp:

services.AddControllersWithViews().AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();

Action:

[HttpPost]
    public void PostMds([FromBody]TestXML request)
    {
        //do your logic..
    }

Test result: enter image description here

enter image description here

Upvotes: 2

Related Questions