Auswählen von XML-Knoten mit dem gleichen Namen, aber unterschiedliches Attribut inneren Text in C #

StackOverflow https://stackoverflow.com/questions/3194082

  •  02-10-2019
  •  | 
  •  

Frage

<item> 
    <title>Aerie Peak</title> 
    <link>http://www.wow-europe.com/realmstatus/index.html#Aerie Peak</link> 
    <description>Aerie Peak - Realm Down - Lang en - Type PvE</description> 
    <category domain="status">Realm Down</category> 
    <category domain="language">en</category> 
    <category domain="type">PvE</category> 

    <category domain="queue">false</category> 
    <guid isPermaLink='false'>EU5-REALM15</guid> 
</item>

ich brauche title und status auszuwählen. Hier ist, was ich kam mit:

string uri = "http://www.wow-europe.com/realmstatus/index.xml"; 
XmlDocument doc = new XmlDocument();  
doc.Load(uri);

XmlNodeList nodes = doc.SelectNodes("//item");

foreach (XmlNode node in nodes)
{
   {
   RealmList.Text += node["title"].InnerText + " - " + 
       node.SelectNodes("category[@domain='status']")[0].InnerText;
   }
}

Das gibt System.NullReferenceException: Object reference not set to an instance of an object. an der Auswahllinie, though. Ich bin mir nicht sicher, wie würde ich mich über den Knoten auswählen.

War es hilfreich?

Lösung

Ihr Code funktioniert gut für Ihr Beispiel, aber die Datei http: // www. wow-europe.com/realmstatus/index.xml enthält mindestens ein Element Element zum Zeitpunkt des Schreibens, das keine Kategorie Element enthält.

Da dieser Knoten nicht vorhanden ist, erhalten Sie die Ausnahme.

Ich spreche über dieses XML an der Spitze des http: // www. wow-europe.com/realmstatus/index.xml .

<item>
            <title>Alert</title>
            <link>http://www.wow-europe.com/realmstatus/index.html</link>
            <description><p><u><strong>Extended Maintenance 07/07</strong></u></p><p>We will be performing extended maintenance on the realms listed below on Wednesday, July 7th, beginning at 00:01 CEST. The maintenance is scheduled for 24 hours as we prepare for the upcoming expansion. These realms will be playable again at approximately 23:59 on Wednesday, July 7th.</p><p>All realms not listed below will undergo scheduled maintenance beginning at 05:00 CEST and will be available for play at approximately 11:00 CEST. </p><p>Additionally, paid character transfer will be unavailable for the duration of the maintenance.</p><p>[11:10] The maintenance has been extended for all realms. We apologise for any inconvenience this may cause and thank you for your patience while this is being resolved. </p><p>Balnazzar<br />Bloodfeather<br />Darksorrow<br />Defias Brotherhood<br />Earthen Ring<br />Frostwhisper<br />Genjuros<br />Haomarush<br />Hellscream<br />Laughing Skull<br />Lightning's Blade<br />Magtheridon<br />Neptulon<br />Nordrassil<br />Quel'Thalas<br />Ragnaros<br />Ravencrest<br />Runetotem<br />Shadowsong<br />Shattered Hand<br />Silvermoon<br />Skullcrusher<br />Spinebreaker<br />Stormrage<br />Stormreaver<br />Stormscale<br />Sylvanas<br />Terenas<br />The Maelstrom<br />The Venture Co.<br />Thunderhorn</p></description>
            <guid isPermaLink='false'>Alert</guid>

        </item>

Ich denke, das ist, wo Ihr Problem ist.

Andere Tipps

Sie haben Realmlist und 'Text' Mitglied in Realmlist zu initialisieren, als String Standard null ist.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top