我试图在从URL读取XML天气数据。 该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;
}
}
任何想法如何,我可以只提取气象数据和时间?