locked
How can I get body from a post with the same parent node but different child node? RRS feed

  • Question

  • User1052024640 posted

    Hi, now I need to get the body from a post. Here is the code:

    public string GetMsg([FromBody]xml x)
    {
    return "";
    } 
    public class xml { 
    public string ToUserName { get; set; }
    public string FromUserName { get; set; }
    public string CreateTime { get; set; }
    public string MsgType { get; set; }
    public string Content { get; set; }
    public string MsgId { get; set; }
    }

    These codes work well.

    However, the remote server will post several XML to my server with the same parent node but different child nodes for different functions.

    For example:

    <xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>1348831860</CreateTime>
      <MsgType><![CDATA[text]]></MsgType>
      <Content><![CDATA[this is a test]]></Content>
      <MsgId>1234567890123456</MsgId>
    </xml>
    <xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>1357290913</CreateTime>
      <MsgType><![CDATA[voice]]></MsgType>
      <MediaId><![CDATA[media_id]]></MediaId>
      <Format><![CDATA[Format]]></Format>
      <MsgId>1234567890123456</MsgId>
    </xml>
    <xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>1357290913</CreateTime>
      <MsgType><![CDATA[video]]></MsgType>
      <MediaId><![CDATA[media_id]]></MediaId>
      <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
      <MsgId>1234567890123456</MsgId>
    </xml>

    I can only define one 'xml' class in a cs file, now it seems I have to create many cs file and define the 'xml' class in each of them.

    It is so troublesome. And also, I can change the code of the remote server. 

    How can I deal with this? Thank you.

    Saturday, September 21, 2019 6:33 AM

Answers

  • User-474980206 posted
    You can use different classes, and use a class xml attribute to set the root name to “xml”.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 21, 2019 3:27 PM

All replies

  • User-474980206 posted
    You can use different classes, and use a class xml attribute to set the root name to “xml”.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 21, 2019 3:27 PM
  • User1052024640 posted

    Thank you. By using the [XmlRoot("xml")]  ,it works.

    Sunday, September 22, 2019 8:14 AM