2013-05-02 76 views
1

我得到一个当“财产‘的innerText’是只写”试图读取属性值XML属性“的innerText”是只写试图读取属性值

这里的时候,错误是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<products> 
    <product ID="11837"> 
     <price currency="EUR">75.29</price> 
     <properties> 
      <property name="brand"> 
       <value></value> 
      </property> 
    </properties> 
<variations/> 
</product> 
</products> 

要提取我做的代价:它返回

node.SelectSingleNode("price").InnerText 

“75.29”

但当我这样做:

node.Attributes("ID").InnerText 

我得到的错误:

住宅“的innerText”是只写

我看不出有任何理由为什么它是只写的,不知道我可以改变它,以便我可以读取值。

回答

1

这是一个0123'实施的事实,它只支持写入其InnerText属性。你不“更改”,这样就可以读出的值 - 您使用Value属性:

Gets or sets the value of the node.

或者,您也可以通过InnerText访问值,如果你投的XmlAttribute作为XmlNode(基类)。

+0

原来它与基类有关。当我循环浏览结果时: 对于每个节点在nodeList中,我没有声明节点的类型。我注意到这一点,因为IntelliSense并不是暗示我是InnerText或Value方法。我现在在我的循环上面声​​明'Dim node As XmlNode'(我之前删除了它),并且它再次工作。也使用Innertext方法而不是Value方法。谢谢! – Flo 2013-05-02 13:13:46

0

按照MSDN

The concatenated values of the node and all its children. For attribute nodes, this property has the same functionality as the Value property.

你应该只使用Value属性,而不是像这样:

node.Attributes("ID").Value 

或者你可以将其转换为XmlNode然后访问InnerTextXmlNodeXmlAttribute的基类,其InnerText属性是读写而非只写。例如:

CType(node.Attributes("ID"), XmlNode).InnerText 

我不确定为什么它在XmlAttribute类中是只写的。据推测,考虑到全班的内部运作,可能会有一些很好的理由,尽管很难想象这会是什么。奇怪的是,在1.1版本的MSDN文档中实际上表示它是该版本框架中的读/写属性。然后,在版本2.0-4.0中,它将该属性定义为只写,但其描述中显示“获取或设置......”因此,MSDN并没有完全一致。