Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize an object when posting data with RestSharp

I've recently started using RestSharp to consume a REST service which uses XML.

It makes deserializing objects from XML to a collection of custom objects trivial. But my question is what is the best way to reserialize when posting back to the service?

Should I use LINQ-to-XML to reserialize? I tried using the Serializeable attribute and a SerializeToXml utility function, but when I do so it seems to break the deserializing performed by RestSharp.

like image 400
Evan Avatar asked Jul 19 '11 20:07

Evan


1 Answers

I have been able to use attributes to get all of what I need, although my situation is relatively simple. For example, to get it to deserialize nodes with dashes in them, and then to be able to serialize to the same node name I used this:

[XmlElement(ElementName = "short-name")]
[SerializeAs(Name = "short-name")]
public string shortName { get; set; }

So, in your example, serialization doesn't respect [XmlElement("elementName")]. Instead, you will need to use [SerializeAs(Name = "elementName")].

I found this by trolling through the test code in the RestSharp project.

like image 104
tor.flatebo Avatar answered Oct 01 '22 19:10

tor.flatebo