2010-07-07 54 views
3
<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> 

我需要選擇titlestatus。以下是我想出了:選擇C#中具有相同名稱但不同屬性內部文本的XML節點

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

這在選擇行給出System.NullReferenceException: Object reference not set to an instance of an object.,雖然。我不知道我將如何去選擇節點。

+0

錯誤發生在哪裏?當我從文件加載示例XML時,適用於我。只有'node.SelectNodes(「....」)。InnerText'很棘手,因爲如果該節點不存在,該怎麼辦?首先,我會做一個'node.SelectSingleNode()',並且將它存儲到一個XmlNode變量中,並且在訪問它的'.InnerText'屬性之前確保該變量是'!= null' ..... – 2010-07-07 11:35:03

回答

3

對於您的示例,您的代碼正常工作,但文件http://www.wow-europe.com/realmstatus/index.xml在編寫時至少包含一個不包含類別元素的item元素。

因爲該節點不存在,您會得到異常。

我正在談論這個XML在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> 

我認爲這就是您的問題所在。

+0

確實,這是問題所在。我有很多東西需要學習,特別是一些基本的異常檢查。非常感謝你! – JStrange 2010-07-07 12:24:20

0

您還必須在RealmList中初始化RealmList和'Text'成員,因爲字符串默認爲null。

相關問題