2012-04-22 78 views
2

我有位於xml文件:http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml如何解析XML數據以檢索子節點值?

現在我想在temp_c, relative_humidity, wind_string獲取值。

對於我創建了一個類WeatherReader.cs作爲

using System; 
using System.Net; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Xml; 

namespace CNGS 
{ 
    public class WeatherReader 
    { public int Temp; 
     public string Humidity; 
     public string Wind; 
     public string place; 

     private void PopulateWeatherData() 
     { 
      XmlReader reader = XmlReader.Create("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml"); 

      reader.MoveToContent();    

      while (reader.Read()) 
      { 
       if (reader.LocalName == "temp_c") 
       { 
        Temp = Convert.ToInt32(reader.Value); 
       } 
if (reader.LocalName == "relative_humidity") 
       { 
        Humidity=reader.Value; 
       } 
if (reader.LocalName == "wind_string") 
       { 
        Wind= reader.Value; 
       } 


      } 

      reader.Close(); 
     } 
    } 
} 

它是正確的,它會獲取所需的值?

現在,因爲我想在Silverlight頁面中顯示此信息。我試圖創建類weatherreader的對象

WeatherReader Weath = new WeatherReader(); 

但我不知道如何獲取溫度,風值等?沒有什麼像int tmp = Weath.Temp正在工作。

請幫助

我想獲取氣象數據,然後使用它在Silverlight控件上的MainPage,顯示實時天氣報告。

感謝

回答

1

不,這不是你將如何使用XmlReader閱讀文檔。這比事實複雜得多,事實上......太複雜了。

你最好使用LINQ to XML,因爲它會簡單得多。

var xml = "http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml"; 
var doc = XDocument.Load(xml); 
var currentObservation = doc.Element("response").Element("current_observation"); 
var temp = (int)currentObservation.Element("temp_c"); 
var humidity = (string)currentObservation.Element("relative_humidity"); 
var wind = (string)currentObservation.Element("wind_string"); 

如果你想使用XmlReader,那麼你就必須做這樣的事情:

var xml = "http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml"; 
using (var reader = XmlReader.Create(xml)) 
{ 
    var temp = default(int); 
    var humidity = default(string); 
    var wind = default(string); 

    string elementName = null; 
    while (reader.Read()) 
    { 
     switch (reader.NodeType) 
     { 
     case XmlNodeType.Element: 
      elementName = reader.Name; 
      break; 
     case XmlNodeType.Text: 
      switch (elementName) 
      { 
      case "temp_c": 
       temp = reader.ReadContentAsInt(); 
       break; 
      case "relative_humidity": 
       humidity = reader.ReadContentAsString(); 
       break; 
      case "wind_string": 
       wind = reader.ReadContentAsString(); 
       break; 
      } 
      elementName = null; 
      break; 
     } 
    } 
} 
+0

收到錯誤:「無法打開‘http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml’URI參數必須是相對路徑指向Silverlight應用程序的XAP包內的內容如果您需要從任意Uri加載內容,請參閱使用WebClient/HttpWebRequest加載XML內容的文檔。 – HL88 2012-04-22 03:49:46

0

這將是我的版本:

XDocument doc = XDocument.Load("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml"); 
var v = from d in doc.Element("response").Elements("current_observation") select new { Temp = d.Element("temp_c").Value, Humedity = d.Element("relative_humidity").Value, Wind = d.Element("wind_string").Value }; 
foreach (var c in v) 
{ 
    Console.WriteLine(c.Temp); 
} 
0

請嘗試這一點,是不是最優雅的解決方案,但應該工作。

using System; 
using System.Net; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml; 

namespace CNGS 
{ 
    public class WeatherReader 
    { 
     public int Temp; 
     public string Humidity; 
     public string Wind; 
     public string place; 

     public void PopulateWeatherData() 
     { 
      XmlReader reader = XmlReader.Create("http://api.wunderground.com/api/adaebe40743a9ca6/geolookup/conditions/forecast/q/India/Pilani.xml"); 

      bool IsNextTemp = false; 
      bool IsHumidityTemp = false; 
      bool IsWindTemp = false; 

      reader.MoveToContent(); 

      while (reader.Read()) 
      { 
       switch (reader.NodeType) 
       { 
        case XmlNodeType.Element: // The node is an element. 
         if (reader.Name == "temp_c") 
         { 
          IsHumidityTemp = false; 
          IsWindTemp = false; 
          IsNextTemp = true; 
         } 
         else if (reader.Name == "relative_humidity") 
         { 
          IsHumidityTemp = true; 
          IsWindTemp = false; 
          IsNextTemp = false; 
         } 
         else if (reader.Name == "wind_string") 
         { 
          IsHumidityTemp = false; 
          IsWindTemp = true; 
          IsNextTemp = false; 
         } 
         else 
         { 
          IsHumidityTemp = false; 
          IsWindTemp = false; 
          IsNextTemp = false; 
         } 
         break; 
        case XmlNodeType.Text: //Display the text in each element. 
         if (IsHumidityTemp) 
          this.Humidity = reader.Value; 
         else if (IsNextTemp) 
          this.Temp = int.Parse(reader.Value); 
         else if (IsWindTemp) 
          this.Wind = reader.Value; 
         break; 
       } 

      } 

      reader.Close(); 
     } 
    } 
}