我在http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/更新了以下代码。我已经更新了最新版本的RestSharp:
var client = new RestClient("http://carma.org");
var request = new RestRequest("api/1.1/searchPlants", Method.GET);
request.AddParameter("location", 4338);
request.AddParameter("limit", 10);
request.AddParameter("color", "red");
request.AddParameter("format", "xml");
var plants = client.Execute<PowerPlantsDTO>(request);
MessageBox.Show(plants.Count.ToString())
using System.Collections.Generic;
namespace RestTest.Model
{
public class CityDTO
{
public string value { get; set; }
}
public class LocationDTO
{
public CityDTO city { get; set; }
public int zip { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
}
public class item
{
public string name { get; set; }
public LocationDTO location { get; set; }
}
public class PowerPlantsDTO : List<item> { }
}
不幸的是,plants.count是空的,因为plants.data但是返回了一个XML数据(参见下面的screeshots)。有人可以帮助我,不管我错过了什么吗?
截图:
Errmessage显示
“参数计数不匹配。”
返回XML内容:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>49046</id>
<name>WATSON COGEN</name>
<carbon>
<past>4503176.0000</past>
<present>4582168.0000</present>
<future>5401482.0000</future>
</carbon>
<energy>
<past>3827727.0000</past>
<present>3017826.0000</present>
<future>3506896.0000</future>
</energy>
<intensity>
<past>2352.9250</past>
<present>3036.7339</present>
<future>3080.4910</future>
</intensity>
<location>
<continent>
<id>5</id>
<value>North America</value>
</continent>
<country>
<id>202</id>
<value>United States</value>
</country>
<latitude>33.8219</latitude>
<longitude>-118.2633</longitude>
<state>
<id>644</id>
<value>California</value>
</state>
<city>
<id>60769</id>
<value>Carson</value>
</city>
<metroarea>
<id>3203</id>
<value>Los Angeles-Long Beach</value>
</metroarea>
<county>
<id>4338</id>
<value>Los Angeles</value>
</county>
<congdist>
<id>5298</id>
<value>Diane Watson</value>
</congdist>
<zip>90749</zip>
</location>
</item>
<item>
<id>7233</id>
<name>CARSON COGEN</name>
<carbon>
<past>432223.9062</past>
<present>440564.3125</present>
<future>451224.5000</future>
</carbon>
<energy>
<past>461797.6875</past>
<present>348148.4062</present>
<future>355428.0938</future>
</energy>
<intensity>
<past>1871.9189</past>
<present>2530.8989</present>
<future>2539.0481</future>
</intensity>
<location>
<continent>
<id>5</id>
<value>North America</value>
</continent>
<country>
<id>202</id>
<value>United States</value>
</country>
<latitude>33.8759</latitude>
<longitude>-118.2491</longitude>
<state>
<id>644</id>
<value>California</value>
</state>
<city>
<id>60769</id>
<value>Carson</value>
</city>
<metroarea>
<id>3203</id>
<value>Los Angeles-Long Beach</value>
</metroarea>
<county>
<id>4338</id>
<value>Los Angeles</value>
</county>
<congdist>
<id>5433</id>
<value>Juanita Millender-McDonald</value>
</congdist>
<zip>90746</zip>
</location>
</item>
</items>
堆栈追踪:
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at RestSharp.Deserializers.XmlDeserializer.Map(Object x, XElement root)
at RestSharp.Deserializers.XmlDeserializer.HandleListDerivative(Object x, XElement root, String propName, Type type)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)
答案 0 :(得分:1)
在查看堆栈跟踪时,它肯定与反序列化XML的能力有关。您所关注的教程是从2009年开始的,看起来RestSharp已经从它下面改变了一点。
我能够让你的样本工作,并使用'plants.Data`访问植物列表,只做一个小改动:
var plants = client.Execute<PowerPlantsDTO>(request);
变为
var plants = client.Execute<List<item>>(request);
在查看RestSharp代码时,似乎他们在XmlDeserializer中使用泛型参数的名称来确定XML节点名称。出于好奇,我快速查看了使用XmlElement属性来尝试影响XML节点的名称,但该属性不能应用于类。据我所知,您最好的选择是塑造您的DTO以明确匹配预期的回报结构。
希望有所帮助。