开发者

DataContract not using Name property specified in attribute

开发者 https://www.devze.com 2023-02-09 03:01 出处:网络
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 attri开发者_开发问答bute.

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 attri开发者_开发问答bute.

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


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:

  • http://msdn.microsoft.com/en-us/library/e123c76w.aspx
  • http://msdn.microsoft.com/en-us/library/kd1dc9w5.aspx

Hope this helps a bit.

0

精彩评论

暂无评论...
验证码 换一张
取 消