2010-08-22 5 views
2

J'essaie de nombreuses solutions différentes sur ce site et aucune ne semble fonctionner pour moi. Je reçois un fichier XML d'un site Web et il me revient dans une chaîne. En utilisant le code ci-dessous j'ai besoin de lire les nœuds dans la section "entrée" du fichier xml. mais cela revient toujours à "0", ce qui signifie qu'aucun noeud n'a été trouvé. la seule chose à gauche je pense est le fichier XML n'est pas correct?Problème lors de la lecture du noeud XML

toute aide serait super ...

Code

------------------ ------------ ci-dessous:

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

------------------------------ Fichier XML ci-dessous ----------- -------

<?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> 
    [email protected] 
    </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>[email protected]</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>:" 

Répondre

4

C'est parce que votre noeud racine XML a un espace de nom. Ce qui suit devrait fonctionner:

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

OK Je vois maintenant, et oui je reçois la sortie d'un, maintenant je dois percer jusqu'à la balise : J'ai essayé XmlNodeList nœuds = doc.SelectNodes (« m: alimentation/entrée », nsMgr); mais je reçois un compte de zéro, je ne m'attends pas à ce que vous fassiez tout le travail pour moi :-) mais toute direction sur la façon d'explorer les balises dont j'ai besoin serait super et merci à tous pour l'aide. – Mike

+1

Je suppose que vous aurez besoin de "" m: feed/m: entry "'. En ce qui concerne les nœuds à l'intérieur de l'entrée, notez qu'ils ont un espace de noms XML * différent *, que vous devrez ajouter séparément au 'nsMgr'. – Timwi

+0

c'était tout! MERCI BEAUCOUP! J'apprends vraiment beaucoup de choses formidables! Merci encore! – Mike

1

Au lieu d'utiliser des classes régulières System.Xml vous pouvez également utiliser les classes de l'espace de noms System.Xml.Linq. Personnellement, je trouve cela beaucoup plus facile à utiliser.

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

Cela vous donnera une collection de noeuds d'entrée du document.

Questions connexes