与RestSharp发布数据时,序列化一个对象 [英] Serialize an object when posting data with RestSharp

查看:485
本文介绍了与RestSharp发布数据时,序列化一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用RestSharp消耗它使用XML REST服务。

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

这让反序列化对象从XML到自定义对象的集合微不足道。但我的问题是,什么是最好的方法的 reserialize 的回发到该服务时?

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?

我应该使用LINQ到XML来reserialize?我试着用可序列化属性和 SerializeToXml 实用功能,但是当我这样做,这似乎打破执行的反序列化通过RestSharp。

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; }

那么,在你的榜样,系列化不尊重 [的XmlElement(的ElementName)] 。相反,你需要使用 [SerializeAs(NAME =的ElementName)]

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

我发现这个通过测试code在RestSharp项目曳。

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

这篇关于与RestSharp发布数据时,序列化一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆