new XElement ( "EffectFile",
new XElement ( "Effects", this.Effects.Select (
e => new XElement ( "Options", e.Options.Select (
o => new XElement ( "Option", o ) ) ) ) ) )
I am trying to add an attribute to the Option
in the last line called Type
, with a value that contains the type of o
which is of type Object
but I want to store the o.GetType()
value and then use it later in the parsing of the xml file, from which the string value will be casted back to this value.
So I am also not sure how I could do a programmatic cast in code where the cast will look like this:
object option = (object) (typeStoredInXml) o;
but in the end the option
value will be of the actual type, even if it just looks like an object, but not a string, unless the actual type was string.
EDIT: This is how the xml should look like:
<Effect>
<Type>Blur</Type>
<Options>
<Option Type="int">88</Option>
</Options>
</Effect>
The type attribute can look different, I am not sure if o.GetType() would look like that if it was an int, but it's just to give an idea.
Is this the sort of thing you're looking for?
new XElement ( "EffectFile",
new XElement ( "Effects", this.Effects.Select (
e => new XElement ( "Options", e.Options.Select (
o => new XElement ( "Option", o,
new XAttribute("Type", o.GetType() ) ) ) ) ) )
Obviously, this will only work if o
is not null.
By the way, you may want to look into XML Serialization, since it looks like that's basically what you're going for anyway. There are libraries built specifically to help with this sort of thing, so you don't have to manually emit and parse XML.