在C#中选择具有相同名称但不同属性内部文本的XML节点

发布于 2024-09-08 15:22:01 字数 1093 浏览 5 评论 0原文

<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。这就是我想到的:

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:对象引用未设置为对象的实例。。我不确定如何选择节点。

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

I need to select title and status. Here's what I came up with:

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

That gives System.NullReferenceException: Object reference not set to an instance of an object. at the selection line, though. I'm not sure how would I go about selecting the node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-09-15 15:22:01

您的代码适用于您的示例,但文件 http://www.wow-europe .com/realmstatus/index.xml 在撰写本文时至少包含一个不包含类别元素的项目元素。

因为该节点不存在,所以您会得到异常。

我正在谈论 http://www.wow-europe 顶部的 XML .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>

我认为这就是你的问题所在。

Your code works fine for your example, but the file http://www.wow-europe.com/realmstatus/index.xml contains at least one item element at the time of writing that does not contain a category element.

Because that node is not present, you get the exception.

I'm talking about this XML at the top of 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>

I think that's where your problem is.

脸赞 2024-09-15 15:22:01

您还必须初始化 RealmList 和 RealmList 中的“Text”成员,因为字符串默认值为 null。

You have to initialize RealmList and 'Text' member in RealmList too, as string default is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文