获取单个节点的值会产生“...无值"; [英] Get value of single node yields "... value of Nothing"

查看:29
本文介绍了获取单个节点的值会产生“...无值";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XmlNode 对象 rowNode,当我调用 rowNode.OuterXml 时,我得到了这个结果:

I've got an XmlNode object rowNode, and when I call rowNode.OuterXml I get this result:

<Row Nr="1">
  <Område FormulaR1C1="" /> 
  <Position FormulaR1C1="" /> 
  <Lev FormulaR1C1="" /> 
  <Option FormulaR1C1="" /> 
</Row>

我正在尝试使用以下代码获取 Område 的值:

I'm trying to get the value of Område with the following code:

rowNode.SelectSingleNode("/Row/Område").InnerText

然后我得到 Referenced object has a value of 'Nothing'. 没关系,我想,因为它没有价值.但随后我又使用了 XML 所在的 rowNode 对象:

and I get Referenced object has a value of 'Nothing'. That's okay, I guess, because it has no value. But then I do it with another turn of the rowNode object where the XML is:

<Row Nr="2">
  <Område FormulaR1C1="1">1</Område> 
  <Position FormulaR1C1="1">1</Position> 
  <Lev FormulaR1C1="NM">NM</Lev> 
  <Option FormulaR1C1="" /> 
</Row>

而且我仍然得到 Referenced object has a value of 'Nothing'. 我还尝试了其他一些元素 - LevPosition 但我得到相同的Nothing"结果.我做错了什么?

And I still get Referenced object has a value of 'Nothing'. I also tried with some of the other elements - Lev and Position but I get the same "Nothing" result. What am I doing wrong?

推荐答案

问题是由于存在默认命名空间.访问命名空间中元素的一种可能方法是使用 XmlNamespaceManager.您需要将前缀到命名空间uri的映射注册到命名空间管理器,然后在xpath中正确使用注册的前缀:

The problem was due to existence of default namespace. One possible way to access elements in namespace is by using XmlNamespaceManager. You need to register mapping of prefix to namespace uri to the namespace manager, and then use the registered prefix properly in the xpath :

Dim doc As New XmlDocument()
.....
Dim nsManager As New XmlNamespaceManager(doc.NameTable)
nsManager.AddNamespace("d", "your-namespace-uri-here")
Dim result = rowNode.SelectSingleNode("/d:Row/d:Område", nsManager).InnerText

或者如果您在选择元素时不需要考虑命名空间,您可以使用 xpath local-name() 函数忽略它:

Or if you don't need to consider namespaces in selecting the elements, you can just ignore it by using xpath local-name() function :

Dim result = rowNode.SelectSingleNode("/*[local-name()='Row']/*[local-name()='Område']").InnerText

这篇关于获取单个节点的值会产生“...无值";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆