2011-07-18 4 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' 클래스에서 자동 구현 프로퍼티를 시도하십시오 : 'public string Day {get; 설정;}'. 자세한 내용은 http://msdn.microsoft.com/en-us/library/bb384054.aspx를 참조하십시오. – CodeZombie

+0

나는 웹 서비스를 사용하여 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") 
      };