I have some code that parses an xml file using XDocument.
It retrieves the values correctly but some of the values I save to xml is of type Object. So when I read these values from the xml, they come as string values, even though I cast them as objects:
XDocument doc = XDocument.Load ( file );
XElement xe = doc.Element ( "EffectFile" );
xe.Elements ( "Options" ).Any ( )
? xe.Elements ( "Options" ).Select (
o => o.Elements ( "Option" ).Select (
n => ( object ) n.Value ).First ( ) )
: null ) )
The value as it appears in the xml:
...
<Option>88</Option>
...
comes as "88", instead of Object 88.
I understand that when I cast a string to an Object, it will still be a string, but is there a way to make it an Object of the actual value (whatever that might be) but not a string?
Because these are different, isn’t it?:
object value = 88;
object value = (object) "88";
and later when I use my options value stored as object[], I want to be able to do:
int intValue = (int) value;
XML holds strings – if you are sure what you are keeping in what places in your xml, you can parse it to correct type – like Tony Casale wrote for ints.
Or if you can have any objects in some places, you’d have to add attribute which specifies to your xml reading code, to what type this node should be changed after reading.
like this:
Then in your code you will have to check which type to convert each string from value tag.