Building XML request with RestSharp

12,893

By default RestSharp is using its own serializer but it also packs the DotNetSerializer so you achieve your goal by changing the serializer like this:

var request = new RestRequest("theresource", Method.POST) 
{ 
    RequestFormat = DataFormat.Xml, 
    XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer() 
};

Then you can use a list of message objects and decorate it with XmlElement attribute:

public class messages
{
    public string accountreference { get; set; }

    public string from { get; set; }

    [XmlElement("message")]
    public List<message> messageList { get; set; }
}


public class message
{
    public string to { get; set; }

    public string body { get; set; }
}

Then you can change the last bit to add multiple messages:

request.AddBody(
    new messages
    {
        accountreference = "ref",
        from = "from",
        messageList = new List<message>() {
                new message { to = "to1", body = "body1" },
                new message { to = "to2", body = "body2" }
                }
    }); 

which would produce (I got the XML by checking request.Parameters[0].Value):

<?xml version="1.0" encoding="utf-8"?>
<messages>
  <accountreference>ref</accountreference>
  <from>from</from>
  <message>
    <to>to1</to>
    <body>body1</body>
  </message>
  <message>
    <to>to2</to>
    <body>body2</body>
  </message>
</messages>

I guess this is the XML format you've been looking for.

Share:
12,893
Glen Thomas
Author by

Glen Thomas

Currently a software engineer at Intelligent Lilli

Updated on June 16, 2022

Comments

  • Glen Thomas
    Glen Thomas almost 2 years

    I am attempting to work with a REST API using RestSharp and C#. The documentation for the API that I am using gives a sample XML request:

    <?xml version='1.0' encoding='UTF-8'?>  
    <messages>  
     <accountreference>EX0000000</accountreference> 
     <from>07700900654</from>
     <message>  
      <to>07700900123</to>  
      <type>SMS</type>  
      <body>Hello Mr Sands.</body>
     </message>  
     <message>  
      <to>07700900124</to>  
      <type>SMS</type>  
      <body>Hello Mr Mayo.</body>
     </message>  
    </messages>
    

    I am struggling to understand how to build the request in the format that they want (multiple elements called "message")

    I have created these classes for RestSharp to serialize:

    public class messages
    {
        public string accountreference { get; set; }
    
        public string from { get; set; }
    
        public message message { get; set; }
    }
    
    public class message
    {
        public string to { get; set; }
    
        public string body { get; set; }
    }
    

    And here is my RestSharp code:

    var client = new RestClient("http://api.url.com/v1.0")
                             {
                                 Authenticator =
                                     new HttpBasicAuthenticator(
                                     UserName,
                                     Password)
                             };
    
    var request = new RestRequest("theresource", Method.POST) { RequestFormat = DataFormat.Xml };
    
    request.AddBody(
        new messages
            {
                accountreference = Configuration.AccountReference,
                from = Configuration.From,
                message =
                    new message { to = Configuration.Message.To, body = Configuration.Message.Body }
            });
    
    var response = client.Execute(request);
    

    This works great when I have only 1 message element, but I don't know how to create multiple message elements without having them nested in an array, which doesn't work with the API.