我有一个从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 数据(参见下面的截图)。有人可以帮我看看我是否遗漏了什么?
截图:
错误消息显示
“参数计数不匹配。”
并返回 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)