I'm getting a "Property 'InnerText' is WriteOnly" error when trying to read an attribute value
我在尝试读取属性值时遇到“Property'InperText'是WriteOnly”错误
Here's my XML:
这是我的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>
To extract the price I do:
提取我做的价格:
node.SelectSingleNode("price").InnerText
which returns "75.29"
返回“75.29”
But when I do:
但当我这样做时:
node.Attributes("ID").InnerText
I get the error:
我收到错误:
Property 'InnerText' is WriteOnly
属性'InnerText'是WriteOnly
I don't see any reason why it's write-only and don't know how I can change it so I can read the value.
我没有看到任何理由为什么它是只写的,不知道我怎么能改变它所以我可以阅读该值。
2 个解决方案
#1
1
It's a fact of the implementation of XmlAttribute
that it only supports writing to its InnerText
property. You don't "change it" so that you can read the value - you use the Value
property:
实现XmlAttribute的一个事实是它只支持写入其InnerText属性。您没有“更改它”以便您可以读取值 - 您使用Value属性:
Gets or sets the value of the node.
获取或设置节点的值。
Alternatively, you can access the value via InnerText
if you cast the XmlAttribute
as an XmlNode
(its base class).
或者,如果将XmlAttribute强制转换为XmlNode(其基类),则可以通过InnerText访问该值。
#2
0
According to the MSDN:
根据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属性相同的功能。
You should just use the Value
property, instead, like this:
你应该只使用Value属性,如下所示:
node.Attributes("ID").Value
Or you can cast it to an XmlNode
and then access the InnerText
. XmlNode
is the base class for XmlAttribute
, and its InnerText
property is read-write rather than write-only. For instance:
或者您可以将其强制转换为XmlNode,然后访问InnerText。 XmlNode是XmlAttribute的基类,其InnerText属性是读写而不是只写。例如:
CType(node.Attributes("ID"), XmlNode).InnerText
I'm not sure why it's write-only in the XmlAttribute
class. Presumably there must have been some good reason for it, given the internal workings of the class, though it's hard to imagine what that would be. The odd thing is that in the MSDN documentation in version 1.1 actually says that it is a read/write property in that version of the framework. Then, in versions 2.0 - 4.0 it defines the property as write-only, but the description of it says "Gets or sets..." So, the MSDN hasn't exactly been consistent about it.
我不确定为什么它只能在XmlAttribute类中写入。考虑到班级的内部运作,大概肯定有一些很好的理由,尽管很难想象会是什么。奇怪的是,在1.1版的MSDN文档中实际上说它是该版本框架中的读/写属性。然后,在版本2.0 - 4.0中,它将属性定义为只写,但它的描述显示“获取或设置...”因此,MSDN并没有完全一致。
#1
1
It's a fact of the implementation of XmlAttribute
that it only supports writing to its InnerText
property. You don't "change it" so that you can read the value - you use the Value
property:
实现XmlAttribute的一个事实是它只支持写入其InnerText属性。您没有“更改它”以便您可以读取值 - 您使用Value属性:
Gets or sets the value of the node.
获取或设置节点的值。
Alternatively, you can access the value via InnerText
if you cast the XmlAttribute
as an XmlNode
(its base class).
或者,如果将XmlAttribute强制转换为XmlNode(其基类),则可以通过InnerText访问该值。
#2
0
According to the MSDN:
根据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属性相同的功能。
You should just use the Value
property, instead, like this:
你应该只使用Value属性,如下所示:
node.Attributes("ID").Value
Or you can cast it to an XmlNode
and then access the InnerText
. XmlNode
is the base class for XmlAttribute
, and its InnerText
property is read-write rather than write-only. For instance:
或者您可以将其强制转换为XmlNode,然后访问InnerText。 XmlNode是XmlAttribute的基类,其InnerText属性是读写而不是只写。例如:
CType(node.Attributes("ID"), XmlNode).InnerText
I'm not sure why it's write-only in the XmlAttribute
class. Presumably there must have been some good reason for it, given the internal workings of the class, though it's hard to imagine what that would be. The odd thing is that in the MSDN documentation in version 1.1 actually says that it is a read/write property in that version of the framework. Then, in versions 2.0 - 4.0 it defines the property as write-only, but the description of it says "Gets or sets..." So, the MSDN hasn't exactly been consistent about it.
我不确定为什么它只能在XmlAttribute类中写入。考虑到班级的内部运作,大概肯定有一些很好的理由,尽管很难想象会是什么。奇怪的是,在1.1版的MSDN文档中实际上说它是该版本框架中的读/写属性。然后,在版本2.0 - 4.0中,它将属性定义为只写,但它的描述显示“获取或设置...”因此,MSDN并没有完全一致。