无法反序列化从 API 获取的 Xml
本文关键字:获取 Xml API 反序列化 | 更新日期: 2023-09-27 18:34:51
我正在使用世界天气在线API来获取特定位置的天气。我的问题是,当我尝试反序列化来自 API 响应流的 XML 输出时,出现错误:XML 文档 (1,1( 有问题。
Uri apiURL = new Uri(@"http://api.worldweatheronline.com/free/v1/weather.ashx?q=Dhaka&format=xml&num_of_days=1&date=today&key=jzb88bpzb5yvaegukmq97mee");
Stream result = RequestHandler.Process(apiURL.ToString());
XmlSerializer des = new XmlSerializer(typeof(LocalWeather));
StreamReader tr = new StreamReader(result);
Object obj = des.Deserialize(tr);
LocalWeather data = (LocalWeather)obj;
来自 Web API 的示例 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<request>
<type>City</type>
<query>Dhaka, Bangladesh</query>
</request>
<current_condition>
<observation_time>01:57 PM</observation_time>
<temp_C>33</temp_C>
<temp_F>91</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl>
<![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[Clear ]]>
</weatherDesc>
<windspeedMiles>2</windspeedMiles>
<windspeedKmph>4</windspeedKmph>
<winddirDegree>77</winddirDegree>
<winddir16Point>ENE</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>76</humidity>
<visibility>10</visibility>
<pressure>1006</pressure>
<cloudcover>2</cloudcover>
</current_condition>
<weather>
<date>2013-10-11</date>
<tempMaxC>36</tempMaxC>
<tempMaxF>97</tempMaxF>
<tempMinC>25</tempMinC>
<tempMinF>77</tempMinF>
<windspeedMiles>5</windspeedMiles>
<windspeedKmph>8</windspeedKmph>
<winddirection>ENE</winddirection>
<winddir16Point>ENE</winddir16Point>
<winddirDegree>65</winddirDegree>
<weatherCode>113</weatherCode>
<weatherIconUrl>
<![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[Sunny]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
</data>
当地天气等级:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace APISample
{
public class LocalWeather
{
public Data data { get; set; }
}
public class Data
{
public List<Current_Condition> current_Condition { get; set; }
public List<Request> request { get; set; }
public List<Weather> weather { get; set; }
}
public class Current_Condition
{
public DateTime observation_time { get; set; }
public DateTime localObsDateTime { get; set; }
public int temp_C { get; set; }
public int windspeedMiles { get; set; }
public int windspeedKmph { get; set; }
public int winddirDegree { get; set; }
public string winddir16Point { get; set; }
public string weatherCode { get; set; }
public List<WeatherDesc> weatherDesc { get; set; }
public List<WeatherIconUrl> weatherIconUrl { get; set; }
public float precipMM { get; set; }
public float humidity { get; set; }
public int visibility { get; set; }
public int pressure { get; set; }
public int cloudcover { get; set; }
}
public class Request
{
public string query { get; set; }
public string type { get; set; }
}
public class Weather
{
public DateTime date { get; set; }
public int tempMaxC { get; set; }
public int tempMaxF { get; set; }
public int tempMinC { get; set; }
public int tempMinF { get; set; }
public int windspeedMiles { get; set; }
public int windspeedKmph { get; set; }
public int winddirDegree { get; set; }
public string winddir16Point { get; set; }
public string weatherCode { get; set; }
public List<WeatherDesc> weatherDesc { get; set; }
public List<WeatherIconUrl> weatherIconUrl { get; set; }
public float precipMM { get; set; }
}
public class WeatherDesc
{
public string value { get; set; }
}
public class WeatherIconUrl
{
public string value { get; set; }
}
}
您需要更新类以匹配 XML 结构的架构(这些架构区分大小写(。
您可以使用 System.Xml.Serialization 中的属性从现有文件开始。
[XmlRoot("data")]
public class Data {
// and so on..
}
或者,您可以使用 XSD 工具按照以下步骤为您生成类。
- 为从服务返回的 XML 创建架构(Xml -> 创建架构(
- 在VS Studio工具中运行以下命令:XSD XmlSchema.xsd/c(其中XmlSchema.xsd是在步骤1中生成的架构(。
现在设置模型的方式是,将 LocalWeather 作为根,而在实际的 XML 中,根是数据。这就是您收到"无效根节点"错误的原因。所以,而不是
XmlSerializer des = new XmlSerializer(typeof(LocalWeather));
StreamReader tr = new StreamReader(result);
Object obj = des.Deserialize(tr);
LocalWeather data = (LocalWeather)obj;
尝试
XmlSerializer des = new XmlSerializer(typeof(Data));
StreamReader tr = new StreamReader(result);
Object obj = des.Deserialize(tr);
Data data = (Data)obj;