我从一个网站上得到了一个xml文件,它以字符串的形式返回给我。
使用下面的代码,我需要读取xml文件“entry”部分中的节点。
但它总是显示为“0”,表示找不到节点。我认为唯一剩下的是XML文件不正确?
任何帮助都会很好。。。
------------------代码如下-----------:
//gets the xml file
string WeatherXML = HttpPost("http://weather.gov/alerts-beta/wwaatmget.php?x=MIC159", "");
//create a xmldoc object..
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
//load the object with the xml file from the web...
doc.LoadXml(WeatherXML);
//go to the main node..
XmlNodeList nodes = doc.SelectNodes("/feed/entry");
//I also tried ....
//doc.SelectNodes("//feed/entry");
//doc.SelectNodes("/entry");
//doc.SelectNodes("//entry");
//loop through the nodes (here is where the nodelist is always empty..
foreach (XmlNode node in nodes)
{
string msgType = node["cap:msgType"].InnerText;
string areaDesc = node["cap:areaDesc"].InnerText;
string summary = node["summary"].InnerText;
string title = node["title"].InnerText;
string link = node["link"].InnerText;
}
------------------------------下面的XML文件------------------
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'no'?>
<!--
This atom/xml feed is an index to active advisories, watches and warnings issued
by the National Weather Service. This index file is not the complete Common
Alerting Protocol (CAP) alert message. To obtain the complete CAP alert,
please follow the links for each entry in this index. Also note the CAP
message uses a style sheet to convey the information in a human readable
format. Please view the source of the CAP message to see the complete data
set. Not all information in the CAP message is contained in this index of
active alerts.
-->
<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- http-date = Sun, 22 Aug 2010 07:06:00 GMT -->
<id>http://www.weather.gov/alerts-beta/wwaatmget.php?x=MIC159</id>
<generator>
NWS CAP Server
</generator>
<updated>2010-08-22T19:06:00-04:00</updated>
<author>
<name>
w-nws.webmaster@noaa.gov
</name>
</author>
<title>
Current Watches, Warnings and Advisories for Van Buren (MIC159) Michigan Issued by the National Weather Service
</title>
<link href='http://www.weather.gov/alerts-beta/wwaatmget.php?x=MIC159'/>
<entry>
<id>http://www.weather.gov/alerts-beta/wwacapget.php?x=MI20100822190600IWXRipCurrentStatementIWX20100823060000MI</id>
<updated>2010-08-22T15:06:00-04:00</updated>
<published>2010-08-22T15:06:00-04:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Rip Current Statement issued August 22 at 3:06PM EDT expiring August 23 at 2:00AM EDT by NWS NorthernIndiana http://www.crh.noaa.gov/iwx/</title>
<link href="http://www.weather.gov/alerts-beta/wwacapget.php?x=MI20100822190600IWXRipCurrentStatementIWX20100823060000MI"/>
<summary>...RIP CURRENT RISK REMAINS IN EFFECT UNTIL 2 AM EDT /1 AM CDT/ MONDAY... ...HIGH RISK OF RIP CURRENTS... HIGH WAVES ALONG THE SHORELINE WILL BRING AN INCREASED RISK OF RIP CURRENTS INTO THE EARLY MORNING HOURS OF MONDAY...CREATING DANGEROUS SWIMMING CONDITIONS.</summary>
<cap:effective>2010-08-22T15:06:00-04:00</cap:effective>
<cap:expires>2010-08-23T02:00:00-04:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency></cap:urgency>
<cap:severity></cap:severity>
<cap:certainty></cap:certainty>
<cap:areaDesc>Berrien; Cass; La Porte; St. Joseph; Van Buren</cap:areaDesc>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>018091 018141 026021 026027 026159</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.CON.KIWX.RP.S.0017.000000T0000Z-100823T0600Z/</value>
</cap:parameter>
</entry>
</feed>:"