3

I've read a few questions on SO but the solutions are all for ASP.NET webApi not dotnet core.

I've added xml support in my Startup.cs

services
       .AddMvc()
       .AddXmlSerializerFormatters();

Here's my controller method:

[Route("main")]
    [HttpPost]
    public string MainPost([FromBody]MessageModel msg)
    {
        _log.LogInformation("body msg ="+msg.Content);
        return "test";
    }

Here's my XML

 <xml>
 <ToUserName>toUser</ToUserName>
 <FromUserName>FromUser</FromUserName>
 <CreateTime>1348831860</CreateTime>
 <MsgType>text</MsgType>
 <Content>test content</Content>
 <MsgId>1234567890123456</MsgId>
</xml>

Here's my Model class:

[XmlRoot("xml")]
public class MessageModel
{

    public string ToUserName { get; set; }
    public string FromUserName { get; set; }
    public DateTime CreateTime { get; set; }
    public string MessageType { get; set; }
    public string Content { get; set; }
    public int MsgId { get; set; }
}

When I send post request(with header "application/xml") to this route, it always gives me null for MessageModel msg

What am I missing here?

I know there's a DataContract thing but I can't control the xml format sending to my server so I need a way to bind xml in the format stated above to my object.

Any help or lead to any document will be much appreciated.

MiDaa
  • 1,149
  • 1
  • 11
  • 25
  • Are you getting an exception? Add a try/catch to code. The CreateTime is going to give an exception. I suspect this could be a cause to the issue. The date is not a standard datetime format so you need to add the the class a get/put property for the datetime to parse correctly. – jdweng Jun 25 '17 at 06:40
  • Try `[XmlRoot("MessageModel")]` – CodingYoshi Jun 25 '17 at 06:41
  • @jdweng I only get exception for accessing a null 'msg' object, and i just tried to change the type CreateTime to string so the datetime parsing should not affect the binding. but unluckily it still gives me null for msg... – MiDaa Jun 25 '17 at 06:44
  • @CodingYoshi Sorry i just tried... still null – MiDaa Jun 25 '17 at 06:45
  • @jdweng You're awesome, the idea you just gave me helped me solve the problem, it is indeed the problem about parsing the element value and storing into field. The msg object starts to appear after i changed every field in the model class to string. Please do write an answer for this question, you deserve the reputation! – MiDaa Jun 25 '17 at 06:54
  • `int.MaxValue` is `2,147,483,647`. Your `MsgId` is much larger than that. Perhaps using `long` instead and combining that with the date parsing issue would fix the problem. Check either answer to this question for a possible way to work around your date problem: https://stackoverflow.com/q/5963423/491907 – pinkfloydx33 Jun 25 '17 at 09:25
  • @pinkfloydx33 yes you're right. Thanks for helping me out! – MiDaa Jun 25 '17 at 16:42

1 Answers1

1

I've found the problem thanks to @jdweng 's comment which gives me a idea on what might go wrong.

The problem is that there are some elements in the XML cannot be serialized to the designated type in my model class. They're DateTime CreateTime and int MsgId.

So changing all the fields to string solved problem.

I don't know whether why isn't dotnet core tells us about this instead of just giving us null if the binding fails due to this kind of reason, this is definitely something they can improve on.

MiDaa
  • 1,149
  • 1
  • 11
  • 25