k4st0r42
k4st0r42

Reputation: 1252

WCF : [XmlElement, typeof] in DataMember attribute

I want to do something like :

[XmlElement("Foo1", typeof(Foo1Type))]
[XmlElement("Foo2", typeof(Foo2Type))]
public object Foo { get; set; }

I want to do this with the DataContractSerializer for WCF. Something similar in Datamember attribute.

This is the equivalent of xsd:Choice.

I aim to assign dynamically tags according to their types.

Thank you.

Upvotes: 2

Views: 2050

Answers (2)

Alexandre Nascimento
Alexandre Nascimento

Reputation: 183

I had to change the ServiceContract as follows:

[ServiceContract(Name="ChoiceService", Namespace="http://dev.janus-net.de/example/choice")]
[XmlSerializerFormat(Style = OperationFormatStyle.Document)]
public interface IChoiceService

And add Serializable in DataContract:

[DataContract(Namespace = "http://...", Name = "yourContract")]
[Serializable]

The complet example are in: http://www.janus-net.de/2007/08/07/using-xschoice-in-wcf-services/

Upvotes: 0

Seymour
Seymour

Reputation: 7067

According to the following MSDN link, the DataContractSerializer does not support mapping XSD choice to the CLR equivalent types.

choice  |  Forbidden

Forbidden. The DataContractSerializer does not support importing a schema using the feature. For example, Svcutil.exe, when accessing a WSDL with a schema that uses such a feature, falls back to using the XmlSerializer instead. This is by default.

http://msdn.microsoft.com/en-us/library/ms733112(v=vs.110).aspx

Note: You may be able to work around the issue using Data Contract Known Types. http://msdn.microsoft.com/en-us/library/ms730167.aspx

Upvotes: 3

Related Questions