2013-05-10 105 views
6

我想從URL中讀取XML中的天氣數據。 XML看起來像這樣:C#從XML中提取數據

<weatherdata> 
<location>...</location> 
<credit>...</credit> 
<links>...</links> 
<meta>...</meta> 
<sun rise="2013-05-11T04:49:22" set="2013-05-11T21:39:03"/> 
<forecast> 
<text>...</text> 
<tabular> 
<time from="2013-05-11T01:00:00" to="2013-05-11T06:00:00" period="0"> 
<!-- 
Valid from 2013-05-11T01:00:00 to 2013-05-11T06:00:00 
--> 
<symbol number="2" name="Fair" var="mf/02n.03"/> 
<precipitation value="0" minvalue="0" maxvalue="0.1"/> 
<!-- Valid at 2013-05-11T01:00:00 --> 
<windDirection deg="173.8" code="S" name="South"/> 
<windSpeed mps="4.2" name="Gentle breeze"/> 
<temperature unit="celsius" value="9"/> 
<pressure unit="hPa" value="1004.2"/> 
</time> 
</tabular> 
</forecast> 
<observations>...</observations> 
</weatherdata> 

我對XML中的預測數據感興趣。我想從時間到時間,然後是天氣數據。例如,溫度是這樣寫的XML:

<temperature unit="celsius" value="9"/> 

我想這樣的事情來提取數據:

string fromTime = time from(the attribute in the xml); 
        string fromTime =time to(the attribute in the xml); 
        string name = temperature(the attribute in the xml); 
        string unit =unit(the attribute in the xml); 
        int value = value(the attribute in the xml); 

我創建示例代碼,能夠讀取的一切,但我不不知道如何提取我需要的數據。我現在的代碼看起來像這樣:

 String URLString = "http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml"; 
     XmlTextReader reader = new XmlTextReader(URLString); 

     while (reader.Read()) 
     { 
      switch (reader.NodeType) 
      { 
       case XmlNodeType.Element: // The node is an element. 
        Console.Write("" + reader.Name); 

        while (reader.MoveToNextAttribute()) // Read the attributes. 
         Console.Write(" " + reader.Name + "='" + reader.Value + "'"); 
        Console.Write("\n"); 
        Console.WriteLine("------------------------------"); 
        break; 
       case XmlNodeType.Text: //Display the text in each element. 
        Console.WriteLine(reader.Value); 
        break; 
       case XmlNodeType.EndElement: //Display the end of the element. 
        Console.Write("</" + reader.Name); 
        Console.WriteLine(">"); 
        break; 

      } 
     } 

任何想法如何我只能提取天氣數據和時間?

+1

您是否僅限於使用.Net 2.0或更早版本?如果沒有,我會建議使用Linq到XML。 – Gjeltema 2013-05-10 22:49:02

回答

6

使用LINQ到XML

XDocument X = XDocument.Load("http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml"); 

var forecast = X.Element("weatherdata").Element("forecast"); 
var location = forecast.Descendants("location").Attributes("name").FirstOrDefault().Value; 
var tempData = forecast.Element("tabular").Elements("time"); 

//This is what you need 
var data = tempData.Select(item=> 
      new{ 
       from = Convert.ToDateTime(item.Attribute("from").Value), 
       to = Convert.ToDateTime(item.Attribute("to").Value), 
       temp = item.Element("temperature").Attribute("value").Value 
      }); 


//Or you can do a foreach if you need to 
foreach (var item in tempData) 
{ 
     DateTime from = Convert.ToDateTime(item.Attribute("from").Value); 
     var temp = item.Element("temperature").Attribute("value").Value; 
} 

我還沒有填充一切。我希望你知道如何使用它。

+0

非常感謝我所需要的! – user1810659 2013-05-10 23:03:45

+0

如何爲我的XML文件執行相同的操作:http://stackoverflow.com/questions/24268245/how-to-extract-every-occurence-of-tags-in-an-xml-file – Si8 2014-06-17 16:05:06

4

使用XElementSystem.Xml.Linq

XElement all = XElement.Load(reader); 
var values = all.Decentents("forecast").Select(fc => { 
    XElement time = fc.Element("time"); 
    XElement temp = fc.Element("temperature"); 
    return new Tuple<string, string, string, string>(
        time.Attribute("from").Value, 
        time.Attribute("to").Value, 
        temperature.Attribute("unit").Value, 
        temperature.Attribute("value").Value);}); 
+0

謝謝!幫助:) – user1810659 2013-05-10 23:05:27