0
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.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

1 Answers1

2

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.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thanks yep, but are you still storing `o` too? I want to store both o and type, but type being an attribute. – Joan Venge Mar 02 '11 at 00:20
  • 1
    @Joan Venge: What would you like the final XML to look like? `o` would look very different depending on whether it's an `int` or a `Customer`. – StriplingWarrior Mar 02 '11 at 00:21
  • Thanks I edited the question to show the xml. Sorry `o` is option's value, so 88 in this case. – Joan Venge Mar 02 '11 at 00:25
  • You don't need to modify your code to save Effect.Type, but just need to see how to save both o (88) and o.GetType() as attribute. Your code doesn't save o being the option value, right? – Joan Venge Mar 02 '11 at 00:27
  • 1
    @Joan Venge: My updated code includes "o". This will obviously only work as you expect if "o" is a very simple type that can be Parsed from its own ToString() output. – StriplingWarrior Mar 02 '11 at 00:34
  • Thanks it works. I just didn't know params element in XElement ctor would allow this. So after the 2nd parameter, everything is passed as attribute? Or does XElement know what to do based on what's passed in the incoming parameters? – Joan Venge Mar 02 '11 at 00:37
  • 1
    XElement will look for other "X" classes (`XNamespace`, `XCData`, etc.), and render those in its own way. Any other objects will have the results of their `ToString()` calls appended onto one another to be used as the contents of the element. – StriplingWarrior Mar 02 '11 at 00:45