无法反序列化从api获取的Xml

时间:2013-10-11 13:58:09

标签: c# xml xml-serialization

我正在使用World Weather Online 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>

LocalWeather类:

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

2 个答案:

答案 0 :(得分:2)

您需要更新类以匹配XML结构的架构(这些是区分大小写的)。

您可以使用System.Xml.Serialization中的属性开始使用现有文件。

[XmlRoot("data")]
public class Data { 
// and so on..
}

或者您可以使用XSD工具按照这些步骤为您生成类。

  1. 从服务(Xml - &gt;创建架构)
  2. 创建XML返回的架构
  3. 在VS Studio工具中运行以下命令:XSD XmlSchema.xsd / c(其中XmlSchema.xsd是步骤1中生成的架构)。

答案 1 :(得分:0)

现在设置模型的方式是,将LocalWeather作为根,而在实际的XML中,根是Data。这就是为什么你得到“无效的根节点”错误。所以,而不是

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;