我的 XML 文档格式如下:
<response>
<forecast>
<txt_forecast>
<forecastdays>
<forecastday>
<period>0</period>
<fctext>Sunny</fctext>
</forecastday>
<forecastday>
<period>1</period>
<fctext>Cloudy></fctext>
</forecastday>
</forecastdays>
</txt_forecast>
</forecast>
</response>
我现在的代码是:
public String getcurrentForecast()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://api.wunderground.com/api/74e1025b64f874f6/forecast/conditions/q/zmw:00000.1.95784.xml");
XmlNode node = doc.DocumentElement.SelectSingleNode("/response/forecast/txt_forecast/forecastdays/forecastday");
我是 XML 阅读新手,我花了 2 天时间尝试理解各种来源的示例。我想在我的代码中使用“XMLDocument”属性。
如何选择“peroid 0”节点并获取“fctext”文本?
像这样:
XmlDocument doc = new XmlDocument();
doc.Load("http://api.wunderground.com/api/74e1025b64f874f6/forecast/conditions/q/zmw:00000.1.95784.xml");
XmlNode node = doc.DocumentElement.SelectSingleNode("/response/forecast/txt_forecast/forecastdays/forecastday[period=0]/fcttext");
String Forecast = node.InnerText;
[更新:] 这里有一个免费实用程序可以帮助您快速找出正确的 XPath 表达式:http://www.bubasoft.net/product/xpath-builder/ http://www.bubasoft.net/product/xpath-builder/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)