2011-07-18 77 views
0

我試圖用Windows Phone 7創建應用程序,該應用程序顯示來自特定URI的數據,但它不起作用。我是堆棧,請幫助我。 這是我的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中的任何東西。如果它是你的用戶界面,那麼你將需要發佈XAML。 – 1adam12

+0

順便說一句:如果你使用C#3.0及以上版本,嘗試在你的'Weather_Element'類自動實現的屬性: '公共串日{獲得;集;}'。 有關更多信息,請參閱http://msdn.microsoft.com/en-us/library/bb384054.aspx。 – CodeZombie

+0

謝謝你們!但是我想知道如何使用Web服務從URI讀取數據(讓我們假設它是相同的數據) – MarTech

回答

1

您正試圖獲得屬性從一個元素值,而無需屬性。

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

forecast_conditions類型的元件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") 
      };