XML 文档选择 [英] XMLDocument Selection
本文介绍了XML 文档选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 XML 文档格式如下:
The XML document I have is the following format:
<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"属性.
I am new to XML reading, and I have spent 2 days trying to understand examples from various sources. I would like to use the 'XMLDocument' property for my code.
如何选择peroid 0"节点并获取fctext"文本?
How can I select the "peroid 0" node and get the "fctext" text please?
推荐答案
像这样:
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/
[Update:] There is a free utility here that helps you quickly figure out correct XPath expressions: http://www.bubasoft.net/product/xpath-builder/
这篇关于XML 文档选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文