Jason Levens
Jason Levens

Reputation: 194

DataContract not using Name property specified in attribute

I don't know what serializer is being used but I'm seeing some inconsistent behavior during serialization when using the Name property of the datacontract attribute.

Here's an example of what I'm doing:

[XmlRoot(ElementName = "ASerCollection")]
public class SerCollection : List<ColElem>
{
}

[DataContract(Name = "SomethignElse", Namespace = "")]
public class ColElem
{

}

The problem I'm having is that the serialization is generating something like the following XML:

<ASerCollection>
<ColElem />
</ASerCollection>

I'm doing this as part of a WCF Service. Please let me know if there's something I can do differently to force the use of the "Name" attribute of my datacontract.

Thanks

Upvotes: 2

Views: 2964

Answers (1)

Andrei
Andrei

Reputation: 4308

This is because:

  • XMLRootAttribute is used by XmlSerializer
  • DataContractAttribute is used by DataContractSerializer

One does not understand attributes of the other. Depending on what you are trying to do and the relationships between your classes, I think you should read on how to guide the above mentioned serializers and choose the one that suites the needs.

Here is a couple of links:

Hope this helps a bit.

Upvotes: 2

Related Questions