I'm getting a "Property 'InnerText' is WriteOnly" error when trying to read an attribute value
Here's my 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"
But when I do:
node.Attributes("ID").InnerText
I get the error:
Property 'InnerText' is 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.
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:
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).
According to the MSDN:
The concatenated values of the node and all its children. For attribute nodes, this property has the same functionality as the Value property.
You should just use the Value property, instead, like this:
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:
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.
Related
It is quite simple to do it, you write the object down to file, then you read it:
http://corlan.org/2008/09/02/storing-data-locally-in-air/
http://cookbooks.adobe.com/post_Storing_ActionScript_Objects_in_the_Encrypted_Loca-10563.html
My questions are
why when we put [RemoteClass(alias="foo.Bar")] into VO, it can be
cast automatically (otherwise the type of the deserialized object is
Generic Object with correct properties data inside it)?
Is there another way to achieve it without RemoteClass tag? (Using metadata tag is preference)
Thanks
The answer is in the page you linked to:
Note that the alias is a key that is stored with the class instance
and links the class definition with the specific object that is stored
in the ByteArray when an instance of that object is serialized. This
key can be any unique string identifying this class, but convention is
to use the fully normalized package and class name.
That's why you get a generic object if you omit the alias - the deserialization method does not know what to do with the data, unless you specify to which class the values should be mapped.
Yes, there is: registerClassAlias() does the same thing. But the metadata tag is much prettier to read ;)
I have some incoming XML documents, I need to check attributes that are configurable in this coming XML and if the attribute is not there in the XML document being checked add it.
The attributes to be checked will be specified in the app.config file
OrderRequest.orderDetails.orderSummary.testCount, I need to check if the testCount attribute exists under the element OrderRequest. If No add it along with it’s value which is also specified in the app.config
INCOMING XML --> Read the attributes from the app.config which need to be checked in this XML file --> Read the incoming XML using XDocument --> If the attribute is not there in the incomg XML add it to the XML.
Any ideas how I can go on this.
I wrote a series of articles on how to create custom configuration sections, which can be found here: http://dotnetslackers.com/articles/CustomConfiguration/default.aspx
Is this a static development task? What I mean is, are you only testing the testCount attribute, or is this generic to say loop through the element type and check all of its attributes to ensure they are there?
Either way, essentially you have to find an element, get its name, access your custom configuration section and lookup the name, use the GetAttribute method to check for attribute existence, and append a new attribute to the element if not.
HTH.
Please explain me what is the difference between XML and XMLList and XMLListCollection. If possible in simple words with example. Thanks in advance.
First, links to Flex 3 Language Reference - a must have bookmark for looking this stuff up.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
XML
XMLList
XMLListCollection
Secondly, I'd say check the Examples link for XMLList as that gives a good working example between the difference of XML and XMLList.
Use XML when you want to create a variable with a value of an XML document.
var mybooks:XML = <books>
<book>
<title>Book1</title>
</book>
<book>
<title>Book2</title>
</book>
</books>;
Use XMLList to create subsets of data from an XML variable.
var mybookTitles:XMLList = mybooks.title;
Finally, an XMLListCollection class is basically a helper class for taking your XML or XMLList object and using it in a control.
Take this snippet from mx.core.Repeater docs on its dataProvider property
If you set it to an XML or XMLList, it is converted into an XMLListCollection.
Hope this helps
An XMLListCollection is probably what you want to use. It has data binding and works well as a datasource in a datagrid.
An XMLListCollection is built from an XMLList. But after you have made an XMLListCollection from your XMLList, you rarely (if ever) use the XMLList again.
I rarely use XML. I think XML is mostly for backwards compatibility and that you are encouraged to use XMLList and XMLListCollection instead.
please i have a namespaced Xml structure :
<rootlevel xmlns="http://www.wigo.org/simo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wigo.org/wigo ./schemas/lexicon.xsd">
<name>simulation</name>
<num_vars>
<variable>
<name>BMASS_COLLECT_PROP_NAT_REM</name>
</variable>
</num_vars>
now, i have a labelFunction
that passes in the xml..as an object but when i ran the programme, the tree view has each node, name, num_vars and variable as branches but the content in each of them was null and blank.
So, my question is how can i properly display the namespaced XML on a tree view such that it shows every branches and elements in each branch?
private function treeLabel(item:Object):String{
var label:String = item.localName();
return label;
}
It might be a good idea to parse XML into a strong-typed object like in this post.
I'm maintaining an API that has deprecated some public static fields.
CheckStyle complains loudly about these but I'd rather have it ignore them completely since I've dealt with the problem by marking the fields as deprecated.
Specifically, the library has constants for enumeration (public static final) but the they are not marked as final. CheckStyle will complain about them, but I can't just change them to final without breaking the contract.
My plan is to mark them as deprecated and then delete them later. But marking them as deprecated doesn't remove them from the CheckStyle Report.
I have two options for you:
suppress the warning for each line manually
This approach is less flexible as you have to maintain the suppression configuration each time you are shifting lines. But you can handle each occurrence individually.
<suppress checks="YOURCHECK" files=".*YOURCLASS\.java" lines="YOURLINES"/>
I don't know which check is causing your problem, so you have to replace YOURCHECK with the proper name. YOURCLASS names the java file, which contains the deprecated code, but you can insert .* to apply it to every file. YOURLINES is a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer.
use a comment to advise checkstyle to ignore warnings
This solution allows you to deactivate checks for all deprecated members at once. But you have to follow a strict convention. You have to add a #deprecated comment to those members (, what you possibly do already) at the very last position, because this filter has a strict range of lines.
/**
* #deprecated I don't like this anymore.
*/
public static String EXAMPLE = "example";
This solutions needs a change within your configuration file. First you have to add FileContentsHolder as a child to TreeWalker.
<module name="TreeWalker">
...
<module name="FileContentsHolder"/>
...
</module>
Now you may configure the SuppressWithNearbyCommentFilter which is part of the Checker module.
<module name="Checker">
...
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat" value=".*deprecated.*"/>
<property name="checkFormat" value=".*"/>
<property name="influenceFormat" value="2"/>
</module>
...
</module>
If you decide to ignore only specific checks, adjust the checkFormat attribute. Or if you want to use another comment, change the commentFormat attribute. But it is very important, that you set influenceFormat to the right value. It tells checkstyle within how many lines after the comment it is due to ignore those checks.
P.S.: Note that the Eclipse CheckStyle plugin removes the FileContentsHolder module, when you change the configuration with its user interface, so you must not use it.