2011-07-18 13 views
0

特定のURIのデータを表示するWindows Phone 7でアプリケーションを作成しようとしていますが、機能しません。私はスタックしています、私を助けてください。 これは私のXMLです:WP7のXML読み取りに関する問題

<?xml version="1.0" encoding="utf-8" ?> 
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> 
    <forecast_conditions> 
     <day_of_week data="lun."/> 
     <low data="28"/> 
     <high data="38"/> 
     <icon data="/ig/images/weather/mostly_sunny.gif"/> 
     <condition data="Partiellement ensoleillé"/> 
    </forecast_conditions> 
    <forecast_conditions> 
     <day_of_week data="mar."/> 
     <low data="27"/> 
     <high data="39"/> 
     <icon data="/ig/images/weather/sunny.gif"/> 
     <condition data="Temps clair"/> 
    </forecast_conditions> 
    <forecast_conditions> 
     <day_of_week data="mer."/> 
     <low data="25"/> 
     <high data="38"/> 
     <icon data="/ig/images/weather/mostly_sunny.gif"/> 
     <condition data="Ensoleillé dans l'ensemble"/> 
    </forecast_conditions> 
    <forecast_conditions> 
     <day_of_week data="jeu."/> 
     <low data="24"/> 
     <high data="33"/> 
     <icon data="/ig/images/weather/mostly_sunny.gif"/> 
     <condition data="Ensoleillé dans l'ensemble"/> 
    </forecast_conditions> 
</weather> 

これは私のC#のコードです:あなたは属性なしの要素から属性値を取得しようとしている

namespace WEATHER2 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructeur 
     public MainPage() 
     { 
      InitializeComponent(); 
      XDocument doc = XDocument.Load("Gweather.xml"); 
      var x= from c in doc.Descendants("forecast_conditions") 
      select new Weather_Element() 
      { 
       Day = (string)c.Attribute("day_of_week").Value, 
       Low = (string)c.Attribute("low").Value, 
       High = (string)c.Attribute("high").Value, 
       Condition = (string)c.Attribute("condition").Value 
       }; 
      listBox1.ItemsSource = x; 
     } 

     public class Weather_Element 
     { 
      string day; 
      string low; 
      string high; 
      string condition; 

      public string Day 
      { 
       get { return day; } 
       set { day = value; } 
      } 
      public string Low 
      { 
       get { return low; } 
       set { low = value; } 
      } 
      public string High 
      { 
       get { return high; } 
       set { high = value; } 
      } 
      public string Condition 
      { 
       get { return condition; } 
       set { condition = value; } 
      } 
     } 
    } 
} 
+1

で働いていませんか? 'x'にデータが入力されていないか、UIに何も表示されていないだけですか?あなたのUIであれば、XAMLも投稿する必要があります。 – 1adam12

+0

ところで:C#3.0以上を使用している場合、 'Weather_Element'クラスでAuto-Implemented Propertiesを試してください: ' public string Day {get;セット;} '。 詳細については、http://msdn.microsoft.com/en-us/library/bb384054.aspxを参照してください。 – CodeZombie

+0

ありがとうございました!しかし、Webサービスを使ってURIからデータを読み込む方法が不思議でした(同じデータだとします) – MarTech

答えて

1

。タイプforecast_conditions

var x = from c in doc.Descendants("forecast_conditions") 
select new Weather_Element() 
{ 
    Day = c.Element("day_of_week").Attribute("data").Value, 
    Low = c.Element("low").Attribute("data").Value, 
    High = c.Element("high").Attribute("data").Value, 
    Condition = c.Element("condition").Attribute("data").Value 
}; 

要素c素子day_of_weekを有しています。次に、この要素の属性はdataです。

1

あなたのforecast_conditionsには属性がありません。その代わりに子要素があり、代わりにdata属性を持っています。だからではなく、

 var x= from c in doc.Descendants("forecast_conditions") 
     select new Weather_Element() 
     { 
      Day = (string)c.Attribute("day_of_week").Value, 
      Low = (string)c.Attribute("low").Value, 
      High = (string)c.Attribute("high").Value, 
      Condition = (string)c.Attribute("condition").Value 
      }; 

使用

 var x= from c in doc.Descendants("forecast_conditions") 
     select new Weather_Element() 
     { 
      Day = (string)c.Element("day_of_week").Attribute("data"), 
      Low = (string)c.Element("low").Attribute("data"), 
      High = (string)c.Element("high").Attribute("data"), 
      Condition = (string)c.Element("condition").Attribute("data") 
      }; 
関連する問題