2013-05-10 20 views
6

Tôi đang cố gắng đọc dữ liệu thời tiết từ XML trong một URL. XML trông giống như sau:C# trích xuất dữ liệu từ 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> 

Tôi quan tâm đến dữ liệu dự báo trong XML. Tôi muốn lấy thời gian và thời gian, sau đó là dữ liệu thời tiết. Ví dụ nhiệt độ được viết như thế này trong XML:

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

tôi muốn trích xuất các dữ liệu với một cái gì đó như thế này:

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

Tôi tạo ra mẫu mã mà có thể đọc tất cả mọi thứ nhưng tôi don không biết cách trích xuất dữ liệu tôi cần. Mã tôi hiện có dạng như sau:

 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; 

      } 
     } 

Bất kỳ ý tưởng nào tôi có thể trích xuất dữ liệu thời tiết và thời gian?

+1

Bạn có bị giới hạn sử dụng .Net 2.0 trở về trước không? Nếu không, tôi khuyên bạn nên sử dụng LINQ to XML. – Gjeltema

Trả lời

6

Sử dụng LINQ to 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; 
} 

tôi đã không cư tất cả mọi thứ. Tôi hy vọng bạn có ý tưởng về cách sử dụng nó.

+0

Cảm ơn rất nhiều những gì tôi cần !! – user1810659

+0

làm cách nào để thực hiện tương tự cho tệp XML của mình: http://stackoverflow.com/questions/24268245/how-to-extract-every-occurence-of-tags-in-an-xml-file – Si8

4

Sử dụng XElement từ System.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

Cảm ơn! đã giúp :) – user1810659