【发布时间】:2017-08-19 15:50:45
【问题描述】:
我有下面的 XML,其中预测有很多“时间”节点......超过 20 个,我只留下 3 个作为下面的示例。 我能够返回位置数据以及时间属性,但我在降水和云方面遇到了困难。
<weatherdata>
<location>
<name>Tokyo</name>
<country>JP</country>
</location>
<forecast>
<time from="2017-08-19T12:00:00" to="2017-08-19T15:00:00">
<precipitation unit="3h" value="0.1925" type="rain"/>
<clouds value="overcast clouds" all="88" unit="%"/>
</time>
<time from="2017-08-19T15:00:00" to="2017-08-19T18:00:00">
<precipitation unit="3h" value="0.085000000000001" type="rain"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
<time from="2017-08-19T18:00:00" to="2017-08-19T21:00:00">
<precipitation unit="3h" value="0.7125" type="rain"/>
<clouds value="overcast clouds" all="88" unit="%"/>
</time>
</forecast>
</weatherdata>
我在下面的代码中将我的问题(我知道的问题!)所在的位置标记为 C# 注释。
using (respStream = resp.GetResponseStream())
{
location = new WeatherData.Location.LocationData();
XmlDocument xml = Utility.retrieveXMLDocFromResponse(respStream, "/weatherdata");
location.country = xml.GetElementsByTagName("country")[0].InnerText;
location.name = xml.GetElementsByTagName("name")[0].InnerText;
forecastList = new List<WeatherData.Forecast.Time>();
XmlNodeList xnlNodes = xml.DocumentElement.SelectNodes("/weatherdata/forecast");
foreach (XmlNode xndNode in xnlNodes)
{
WeatherData.Forecast.Time time = new WeatherData.Forecast.Time();
time.from = xndNode["time"].GetAttribute("from");
time.to = xndNode["time"].GetAttribute("to");
// Here I am able to get data for clouds and preciptation, but the loop goes through all the 20 nodes. I just want to return the "preciptation" and "clouds" of the relevant "time" node.
XmlNodeList xnlTNodes = xml.DocumentElement.SelectNodes("/weatherdata/forecast/time");
foreach (XmlNode xmlTimeNode in xnlTNodes)
{
time.clouds = xmlTimeNode["clouds"].GetAttribute("value");
time.precipitation = xmlTimeNode["precipitation"].GetAttribute("type");
}
forecastList.Add(time);
}
}
WeatherData 类仅包含用于存储数据的 getter/setter 方法。下面我的其他两个引用方法做同样的事情,但一个返回一个 XmlNodeList,另一个返回一个 XML 文档。
它们是静态的,所以我暂时不创建对该类的引用:
public static XmlNodeList retrieveXMLResponse(Stream stream, String baseNode)
{
StreamReader reader = null;
XmlElement xelRoot = null;
XmlNodeList xnlNodes = null;
try
{
reader = new StreamReader(stream, Encoding.UTF8);
string responseString = reader.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString);
xelRoot = xmlDoc.DocumentElement;
xnlNodes = xelRoot.SelectNodes(baseNode);
}
finally
{
reader.Close();
}
return xnlNodes;
}
public static XmlDocument retrieveXMLDocFromResponse(Stream stream, String baseNode)
{
StreamReader reader = null;
XmlDocument xmlDoc = null;
try
{
reader = new StreamReader(stream, Encoding.UTF8);
string responseString = reader.ReadToEnd();
xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString);
}
finally
{
reader.Close();
}
return xmlDoc;
}
【问题讨论】:
-
在您的代码中什么不起作用?选择所有时间节点然后访问它的时间节点似乎可以。你还想做什么?只需不要选择第 10 行中的预测节点。