2010-11-30 100 views
1

我可以在Xpath中执行此操作,但不幸的是,对于此特定项目,我无法使用xpath。在.NET中读取xml ref属性

我有以下节点选择:

<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” 代替 “相对” 正常工作。

我敢肯定,我已经选择了正确的节点,以及比赛的innerText,以及attribute.count返回2.

+4

它如何不正常工作?你收到异常或返回一些错误的值? – 2010-11-30 19:29:53

+0

''行是否正确? – BeemerGuy 2010-11-30 19:31:17

回答

2

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


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

0

我会在循环中添加一个中断,并查看属性集合中的内容。另外,对于踢腿,尝试改变“rel”到其他东西,看看它是否解决了这个问题。