2014-10-15 2 views
0

XDocument를 클래스로 변환하고 싶습니다. 평소 xsd 도구로 수업을 만들었지 만 windows-phone-8에서는 지원하지 않는 것 같습니다. 그렇다면 원래 XDocument의 모든 정보가 필요하지 않은 경우 가장 일반적으로이 작업을 수행하는 방법은 무엇입니까?xDocument를 클래스로 자동 변환하는 방법

이 내 원래의 XML입니다 :

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

내 마지막 클래스는 다음과 같이 보일 것이다 :이 내 XDocument.base입니다

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

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

답변

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

그 멋져 보이지만 어떻게 ObjectClasses를 ma로 변경해야합니까? deserialization 작업을 할 수 있습니까? 이 오류가 발생합니다. {System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. 에서 KiteCast.ClassConverter.Start (XDocument xDoc) KiteCast.MainPage. d__0.MoveNext()} –

+0

개체 클래스와 XML을 비교하여 태그를 놓치지 않았는지 확인하십시오. – MatDev8

관련 문제