2009-07-07 20 views
0

:これは私が欲しいものをしていますがXLinq - 私はこのコードを持っている良い方法

XDocument xdoc = XDocument.Load(URI); 
      XElement root = xdoc.Element("forecast"); 
      //get the values into objects 
      forecast = from fc in root.Descendants("simpleforecast").Elements("forecastday") 
         select new DayForcast 
         { 
          Date = new DateTime(int.Parse(fc.Element("date").Element("year").Value), 
           int.Parse(fc.Element("date").Element("month").Value), 
           int.Parse(fc.Element("date").Element("day").Value)), 
          Condition = fc.Element("conditions").Value, 
          Max = double.Parse(fc.Element("high").Element("celsius").Value), 
          Min = double.Parse(fc.Element("low").Element("celsius").Value), 
          Icon = fc.Element("icon").Value, 
          SkyIcon = fc.Element("skyicon").Value 
         }; 

は、私が(「低」)fc.Elementを行うには良い方法があるかどうかを知りたいです。要素( "摂氏")。要素(要素)が1つの要素()であるように、要素を値化します。ここで

は、XMLのサンプルです:あなたは、このXMLフラグメントからDayForcastオブジェクトをデシリアライズするシリアライズの使用を検討する必要があり

<?xml version="1.0" ?> 
<forecast> 
<termsofservice link="http://www.wunderground.com/members/tos.asp#api" /> 
<txt_forecast> 
    <date /> 
    <number /> 
</txt_forecast> 
<simpleforecast> 
    <forecastday> 
    <period>1</period> 
    <date> 
     <day>7</day> 
     <month>7</month> 
     <year>2009</year> 
     <yday>187</yday> 
     <hour>22</hour> 
    </date> 
    <high> 
     <fahrenheit>63</fahrenheit> 
     <celsius>17</celsius> 
    </high> 
    <low> 
     <fahrenheit>54</fahrenheit> 
     <celsius>12</celsius> 
    </low> 
<conditions>Thunderstorm</conditions> 
<icon>tstorms</icon> 
<skyicon>cloudy</skyicon> 

おかげ

答えて

0

より簡潔にしたい場合はXElement.XPathSelectElements("xpathExpression")を使用できますが、行ったことに何も問題はありません。

あなたのコードは、より冗長で読みやすくなっています。

+0

あなたの答えをありがとう! –

1

+0

私はOPがそれを行うLINQの方法を知りたいと思う。 –

+0

こんにちは、お返事ありがとうございます。その結果は、私が逐次化することができないRSSフィードからのものです...私は逐次化にもあまり慣れていません。 –

関連する問題