datacontract

How can you control .NET DataContract serialization so it uses XML attributes instead of elements?

对着背影说爱祢 提交于 2019-11-26 05:35:31
问题 If I have a class marked as a DataContract and a few properties on it marked with DataMember attributes I can serialize it out to XML easily, but it would create output like: <Person> <Name>John Smith</Name> <Email>john.smith@acme.com</Email> <Phone>123-123-1234</Phone> </Person> What I would prefer is attributes, like... <Person Name=\"John Smith\" Email=\"john.smith@acme.com\" Phone=\"123-123-1234\" /> The DataMember attribute allows me to control the Name and Order but not whether it is

DataContract XML serialization and XML attributes

情到浓时终转凉″ 提交于 2019-11-26 04:40:20
问题 Is it possible to deserialize this XML into an object marked with the DataContract attribute? <root> <distance units=\"m\">1000</distance> </root> As you may see there is \"units\" attribute. I don\'t believe that\'s supported. Or am I wrong? 回答1: This can be achieved, but you will have to override the default serializer by applying the [XmlSerializerFormat] attribute to the DataContract. Although it can be done, this does not perform as well as the default serializer, so use it with caution.

When to use DataContract and DataMember attributes?

放肆的年华 提交于 2019-11-26 02:27:04
问题 I am very confused about the DataContract attribute in WCF. As per my knowledge it is used for serializating user defined type like classes. I wrote one class which is exposed at client side like this. [DataContract] public class Contact { [DataMember] public int Roll { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Address { get; set; } [DataMember] public int Age { get; set; } } It is working properly but when I remove DataContract and DataMember it