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.

7
  • 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. Commented Jun 25, 2017 at 6:40
  • Try [XmlRoot("MessageModel")] Commented Jun 25, 2017 at 6: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... Commented Jun 25, 2017 at 6:44
  • @CodingYoshi Sorry i just tried... still null Commented Jun 25, 2017 at 6: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! Commented Jun 25, 2017 at 6:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.