在.NET中读取xml ref属性

时间:2010-11-30 19:27:52

标签: c# xml dom

我可以在Xpath中执行此操作,但不幸的是,对于此特定项目,我无法使用xpath。

我选择了以下节点:

<client href="http://somewebsitehere.com" rel="needthisdata"></client>


clientnode.Attributes["href"].Value 

正确返回“http://somewebsitehere.com”,但

clientnode.Attributes["rel"].Value

不起作用?

我在这里做错了什么?

完全用法:

foreach (XmlNode clientnode in clientnodelist)
{
    MessageBox.Show(clientnode.Attributes["rel"].Value);
}

上面给出了NullReferenceException,而用“href”替换“rel”工作正常。

我确定我选择了正确的节点以及innertext匹配,以及attribute.count返回2。

2 个答案:

答案 0 :(得分:2)

阅读您的代码。您正在使用clientnode作为循环变量,但您引用的是selectednode


如果一个特定节点没有“rel”属性怎么办? NullReferenceException

答案 1 :(得分:0)

我会在循环中添加一个中断并查看我的Attributes集合中的内容。此外,对于踢,尝试将“rel”更改为其他内容以查看它是否解决了问题。

相关问题