2013-05-10 20 views
6

Sto provando a leggere i dati meteo da XML in un URL. L'XML è simile al seguente:C# estrazione dati da 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> 

Sono interessato i dati previsionali nel codice XML. Voglio ottenere il tempo e il tempo per, quindi i dati meteorologici. Ad esempio, la temperatura è scritto così nel XML:

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

Voglio estrarre i dati con qualcosa di simile:

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

ho creato codice di esempio che è in grado di leggere tutto, ma io don so come estrarre solo i dati di cui ho bisogno. Il codice che ho ora è simile al seguente:

 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; 

      } 
     } 

Qualche idea su come estrarre solo i dati meteorologici e l'ora?

+1

Sei limitato all'utilizzo di .Net 2.0 o precedente? In caso contrario, raccomanderei l'uso di Linq in XML. – Gjeltema

risposta

6

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

non ho popolato tutto. Spero che tu abbia l'idea di come usarlo.

+0

Grazie mille esattamente quello di cui avevo bisogno !! – user1810659

+0

come faccio lo stesso per il mio file XML: http://stackoverflow.com/questions/24268245/how-to-extract-every-occurence-of-tags-in-an-xml-file – Si8

4

Utilizzare il XElement da 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

Grazie! che ha aiutato :) – user1810659