如何为Xml反序列化构造我的类

时间:2014-10-16 09:35:44

标签: c# xml xml-serialization xml-deserialization

我想将XDocument反序列化为一个类。

这是我的XDocument:

<data>
  <request>
    <type>LatLon</type>
    <query>Lat 49.88 and Lon 8.65</query>
  </request>
  <current_condition>
    <observation_time>01:06 PM</observation_time>
    <temp_C>19</temp_C>
    <temp_F>65</temp_F>
    <weatherCode>113</weatherCode>
    <weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>
    <weatherDesc><![CDATA[Sunny]]></weatherDesc>
    <windspeedMiles>5</windspeedMiles>
    <windspeedKmph>8</windspeedKmph>
    <winddirDegree>228</winddirDegree>
    <winddir16Point>SW</winddir16Point>
    <precipMM>0.0</precipMM>
    <humidity>61</humidity>
    <visibility>10</visibility>
    <pressure>1009</pressure>
    <cloudcover>20</cloudcover>
  </current_condition>
  <weather>
    <date>2014-10-15</date>
    <tempMaxC>19</tempMaxC>
    <tempMaxF>65</tempMaxF>
    <tempMinC>12</tempMinC>
    <tempMinF>54</tempMinF>
    <windspeedMiles>5</windspeedMiles>
    <windspeedKmph>9</windspeedKmph>
    <winddirection>SW</winddirection>
    <winddir16Point>SW</winddir16Point>
    <winddirDegree>228</winddirDegree>
    <weatherCode>113</weatherCode>
    <weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]></weatherIconUrl>
    <weatherDesc><![CDATA[Sunny]]></weatherDesc>
    <precipMM>1.8</precipMM>
  </weather>
  <weather>
    <date>2014-10-16</date>
    <tempMaxC>17</tempMaxC>
    <tempMaxF>63</tempMaxF>
    <tempMinC>13</tempMinC>
    <tempMinF>56</tempMinF>
    <windspeedMiles>12</windspeedMiles>
    <windspeedKmph>19</windspeedKmph>
    <winddirection>SW</winddirection>
    <winddir16Point>SW</winddir16Point>
    <winddirDegree>233</winddirDegree>
    <weatherCode>353</weatherCode>
    <weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png]]></weatherIconUrl>
    <weatherDesc><![CDATA[Light rain shower]]></weatherDesc>
    <precipMM>4.5</precipMM>
  </weather>
  <weather>
    <date>2014-10-17</date>
    <tempMaxC>17</tempMaxC>
    <tempMaxF>63</tempMaxF>
    <tempMinC>11</tempMinC>
    <tempMinF>51</tempMinF>
    <windspeedMiles>21</windspeedMiles>
    <windspeedKmph>34</windspeedKmph>
    <winddirection>WNW</winddirection>
    <winddir16Point>WNW</winddir16Point>
    <winddirDegree>284</winddirDegree>
    <weatherCode>176</weatherCode>
    <weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png]]></weatherIconUrl>
    <weatherDesc><![CDATA[Patchy rain nearby]]></weatherDesc>
    <precipMM>11.2</precipMM>
  </weather>
  <weather>
    <date>2014-10-18</date>
    <tempMaxC>21</tempMaxC>
    <tempMaxF>69</tempMaxF>
    <tempMinC>12</tempMinC>
    <tempMinF>54</tempMinF>
    <windspeedMiles>5</windspeedMiles>
    <windspeedKmph>8</windspeedKmph>
    <winddirection>SE</winddirection>
    <winddir16Point>SE</winddir16Point>
    <winddirDegree>141</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>
  <weather>
    <date>2014-10-19</date>
    <tempMaxC>23</tempMaxC>
    <tempMaxF>74</tempMaxF>
    <tempMinC>13</tempMinC>
    <tempMinF>56</tempMinF>
    <windspeedMiles>6</windspeedMiles>
    <windspeedKmph>10</windspeedKmph>
    <winddirection>S</winddirection>
    <winddir16Point>S</winddir16Point>
    <winddirDegree>190</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>

这是我的课程: 我的主要类 - namespace = ObjectClasses

[XmlRoot("data")]
public class WindCol
{
    [XmlElement("query")]
    public string requestLocation {get;set;}

    [XmlElement("observation_time")]
    public string time { get; set; }

    [XmlElement("temp_C")]
    public string temp { get; set; }

    [XmlElement("weatherDesc")]
    public string weatherDesc { get; set; }

    [XmlElement("windspeedKmph")]
    public string windspeed { get; set; }

    [XmlElement("winddirDegree")]
    public string windDegree { get; set; }

    [XmlElement("winddir16Point")]
    public string windDir { get; set;}

    [XmlElement("precipMM")]
    public string precipMM { get; set; }

    [XmlElement("humidity")]
    public string humidity { get; set; }

    [XmlElement("visibility")]
    public string visibility { get; set; }

    [XmlElement("pressure")]
    public string pressure { get; set; }

    [XmlElement("cloudcover")]
    public string cloudcover { get; set; }

    [XmlArray("weather")]
    public List<WindDay> windDays{get;set;}
}

我的子类 - namespace = ObjectClasses

[XmlRoot("data")]
public class WindDay
{
    [XmlElement("date")]
    public string day { get; set; }

    [XmlElement("temMaxC")]
    public string tempMax { get; set; }

    [XmlElement("temMinC")]
    public string tempMin { get; set; }

    [XmlElement("windspeedKmph")]
    public string windspeed { get; set; }

    [XmlElement("winddirection")]
    public string windDir { get; set; }

    [XmlElement("winddirDegree")]
    public string windDegree { get; set; }

    [XmlElement("weatherCode")]
    public string weatherCode { get; set; }

    [XmlElement("weatherDesc")]
    public string weatherDesc { get; set; }

    [XmlElement("precipMM")]
    public string precipMM { get; set; }
}

使用这段代码,我尝试反序列化它:

public ObjectClasses.WindCol Start(XDocument xDoc)
    {
        XmlSerializer s = new XmlSerializer(typeof(ObjectClasses.WindCol));
        return (ObjectClasses.WindCol)s.Deserialize(xDoc.CreateReader());
    }

0 个答案:

没有答案