I try many different solution on this site and none seems to work for me . I getting a xml file from a website and it's returned to me in a string.
using the code below I need to read the nodes in the "entry" section of the xml file. but it always comes up "0" meaning no nodes found. the only thing left I think is the XML file is not correct?
any help would be great...
------------------code below ------------:
//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"].Inn开发者_Go百科erText;
}
------------------------------XML file below------------------
<?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>:"
It’s because your XML root node has a namespace. The following should work:
//load the object with the xml file from the web...
doc.LoadXml(WeatherXML);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("m", "http://www.w3.org/2005/Atom");
//go to the main node..
XmlNodeList nodes = doc.SelectNodes("m:feed", nsMgr);
Console.WriteLine(nodes.Count); // outputs 1
Add the "http://www.w3.org/2005/Atom" namespace to the XPath using an XmlNamespaceManager.
Instead of using regular System.Xml
classes you could also use classes from the System.Xml.Linq
namespace. Personally I find these a lot easier to use.
var doc = XDocument.Parse(WeatherXml);
var entryNodes = doc.Descendants(
XName.Get("entry", "http://www.w3.org/2005/Atom"));
This will give you a collection of entry nodes from the document.
精彩评论