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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With