我正在尝试使用 RestSharp 反序列化以下 xml,但我总是得到一个空元素列表。我是基于 REST 的服务的新手,需要一些专家帮助!=)
找到的解决方案:我想通了。您必须明确告诉 RestSharp 正在反序列化哪种数据:
request.OnBeforeDeserialization = resp => {
resp.ContentType = "application/json";
};
完整的 XML:http ://autocomplete.wunderground.com/aq?query=San%20F&format=xml 一些 XML 如下:
<RESULTS>
<name>San Francisco, California</name>
<type>city</type>
<c>US</c>
<zmw>94101.1.99999</zmw>
<tz>America/Los_Angeles</tz>
<tzs>PDT</tzs>
<l>/q/zmw:94101.1.99999</l>
</RESULTS>
这是我的 XMLResults 类:
public class XMLResults
{
public List<name> names {get; set;}
}
public class name
{
public string city {get; set;}
}
这是我的 getWeather 方法:
public void getWeather(string query)
{
var client = new RestClient ();
var request = new RestRequest(Method.GET);
client.BaseUrl = "http://autocomplete.wunderground.com";
request.Resource = "aq";
request.AddParameter ("query", query);
request.AddParameter ("format", "xml");
request.RequestFormat = DataFormat.Xml;
var city = client.Execute<XMLResults>(request);
Console.WriteLine (city.Data.names.Count); // Results in 20
}