Similar attribute to [XmlElement, typeof] in DataMember

Hi

Is there similar to

[XmlElement(ElementName = "MyCommand", Type = typeof(Command1))]

[XmlElement(ElementName = "MyAnotherCommand", Type = typeof(Command2))]

public Command[] Commands{}

in DataMember

Thanks in advance

Javier



Answer this question

Similar attribute to [XmlElement, typeof] in DataMember

  • Balaji K

    Well, not exactly. With KnownType, you will still get the same element names for Command1 and Command2, but you'll still be able to use both types (they'll be distinguished using the xsi:type attribute on the wire). Changing element names at runtime based on the type is a schema "choice" construct, something that the data contract model does not support (unless you get into some form of imperative custom serialization - IXmlSerializable, XmlNode[], etc.)

  • SamTran

    Yes, there is. You can use

    [KnownType(typeof(Command1))]

    [KnownType(typeof(Command2))]

    when you work with XmlFormater and DataContract.

    Indigo Cowboy


  • Similar attribute to [XmlElement, typeof] in DataMember