读取XML节点时出现问题

时间:2010-08-22 21:47:50

标签: c# xml

我在这个网站上尝试了许多不同的解决方案,似乎没有一个对我有用。 我从网站上获取了一个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>:"

3 个答案:

答案 0 :(得分:4)

这是因为您的XML根节点有一个命名空间。以下应该有效:

//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

答案 1 :(得分:1)

使用XmlNamespaceManager将“http://www.w3.org/2005/Atom”命名空间添加到XPath。

答案 2 :(得分:1)

您还可以使用System.Xml.Linq命名空间中的类,而不是使用常规System.Xml类。我个人认为这些更容易使用。

var doc = XDocument.Parse(WeatherXml);
var entryNodes = doc.Descendants(
        XName.Get("entry", "http://www.w3.org/2005/Atom"));

这将为您提供文档中的入口节点集合。

相关问题