首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将xDocument自动转换为类

如何将xDocument自动转换为类
EN

Stack Overflow用户
提问于 2014-10-15 21:17:10
回答 1查看 4.5K关注 0票数 2

我想把一个XDocument转换成一个类。通常我使用xsd-tool创建我的类,但是在windows-phone-8中似乎不支持它。那么,最常见的方法是什么,特别是如果我不需要原始XDocument中的所有信息呢?

这是我的原始xml:

代码语言:javascript
复制
<?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>

这是我的XDocument.base:

代码语言:javascript
复制
<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>

我的最后一个类应该是这样的:

代码语言:javascript
复制
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;}
}
EN

回答 1

Stack Overflow用户

发布于 2014-10-16 16:38:36

代码语言:javascript
复制
public static MyClass FromXml (XDocument xd)
{
   XmlSerializer s = new XmlSerializer(typeof(MyClass));
   return (MyClass)s.Deserialize(xd.CreateReader());
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26383593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档