Raymond Osterbrink
Raymond Osterbrink

Reputation: 540

how to auto-convert xDocument into Class

I'd like to converte a XDocument into a class. Usualy I create my class with the xsd-tool, but it seems its not supported in windows-phone-8. So what is the most common way to do this, expecially if i don't need all the infos from the original XDocument?

this is my original xml:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <request>
        <type>City</type>
        <query>Frankfurt, Germany</query>
    </request>
    <current_condition>
        <observation_time>02:37 PM</observation_time>
        <temp_C>17</temp_C>
        <temp_F>63</temp_F>
        <weatherCode>116</weatherCode>
        <weatherIconUrl>
            <![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png]]>
        </weatherIconUrl>
        <weatherDesc>
            <![CDATA[Partly Cloudy ]]>
        </weatherDesc>
        <windspeedMiles>7</windspeedMiles>
        <windspeedKmph>11</windspeedKmph>
        <winddirDegree>270</winddirDegree>
        <winddir16Point>W</winddir16Point>
        <precipMM>0.1</precipMM>
        <humidity>83</humidity>
        <visibility>10</visibility>
        <pressure>1009</pressure>
        <cloudcover>50</cloudcover>
    </current_condition>
    <weather>
        <date>2014-10-14</date>
        <tempMaxC>19</tempMaxC>
        <tempMaxF>67</tempMaxF>
        <tempMinC>9</tempMinC>
        <tempMinF>48</tempMinF>
        <windspeedMiles>7</windspeedMiles>
        <windspeedKmph>12</windspeedKmph>
        <winddirection>SSW</winddirection>
        <winddir16Point>SSW</winddir16Point>
        <winddirDegree>203</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.4</precipMM>
    </weather>
    <weather>
        <date>2014-10-15</date>
        <tempMaxC>19</tempMaxC>
        <tempMaxF>67</tempMaxF>
        <tempMinC>12</tempMinC>
        <tempMinF>53</tempMinF>
        <windspeedMiles>7</windspeedMiles>
        <windspeedKmph>12</windspeedKmph>
        <winddirection>SW</winddirection>
        <winddir16Point>SW</winddir16Point>
        <winddirDegree>233</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.9</precipMM>
    </weather>
    <weather>
        <date>2014-10-16</date>
        <tempMaxC>17</tempMaxC>
        <tempMaxF>63</tempMaxF>
        <tempMinC>12</tempMinC>
        <tempMinF>53</tempMinF>
        <windspeedMiles>11</windspeedMiles>
        <windspeedKmph>18</windspeedKmph>
        <winddirection>SW</winddirection>
        <winddir16Point>SW</winddir16Point>
        <winddirDegree>229</winddirDegree>
        <weatherCode>266</weatherCode>
        <weatherIconUrl>
            <![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png]]>
        </weatherIconUrl>
        <weatherDesc>
            <![CDATA[Light drizzle]]>
        </weatherDesc>
        <precipMM>1.7</precipMM>
    </weather>
    <weather>
        <date>2014-10-17</date>
        <tempMaxC>19</tempMaxC>
        <tempMaxF>66</tempMaxF>
        <tempMinC>10</tempMinC>
        <tempMinF>50</tempMinF>
        <windspeedMiles>14</windspeedMiles>
        <windspeedKmph>22</windspeedKmph>
        <winddirection>WNW</winddirection>
        <winddir16Point>WNW</winddir16Point>
        <winddirDegree>301</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>9.8</precipMM>
    </weather>
    <weather>
        <date>2014-10-18</date>
        <tempMaxC>21</tempMaxC>
        <tempMaxF>69</tempMaxF>
        <tempMinC>13</tempMinC>
        <tempMinF>55</tempMinF>
        <windspeedMiles>6</windspeedMiles>
        <windspeedKmph>9</windspeedKmph>
        <winddirection>SSE</winddirection>
        <winddir16Point>SSE</winddir16Point>
        <winddirDegree>149</winddirDegree>
        <weatherCode>116</weatherCode>
        <weatherIconUrl>
            <![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png]]>
        </weatherIconUrl>
        <weatherDesc>
            <![CDATA[Partly Cloudy ]]>
        </weatherDesc>
        <precipMM>0.0</precipMM>
    </weather>
</data>

this is my XDocument.base:

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

my final class should look something like this:

class WindClass
{
    public double[] requestLocation {get;set;}

    public List<WindDay> {get;set;}
}
class WindDay
{
    public DateTime day    {get;set;}
    public int tempCmin    {get;set;}
    public int tempCmax    {get;set;}
    public int tempFmin    {get;set;}
    public int tempFmax    {get;set;}
    public int windspeedM  {get;set;}
    public int windspeedK  {get;set;}
    public string windDir  {get;set;}
    public int windDegree  {get;set;}
    public int weatherCode {get;set;}
    public int visibility  {get;set;}
}

Upvotes: 3

Views: 6804

Answers (1)

MatDev8
MatDev8

Reputation: 986

public static MyClass FromXml (XDocument xd)
{
   XmlSerializer s = new XmlSerializer(typeof(MyClass));
   return (MyClass)s.Deserialize(xd.CreateReader());
}

Upvotes: 9

Related Questions