Custom serialization using DataContractSerializer?

If you are writing xml, I wonder whether xml serializer wouldn't be a better choice (it has more granular control over the names, etc) The problem, though, is that XmlSerializer isn't always the biggest fan of generics Additionally - having tried a few options involving XmlArray XmlArrayItem etc... it looks very hard to get it to the format you want... plus it isn't easy to guess what you mean by the T1 T2 but the following may come close: XmlRoot("response") public class MyResponse : MyCollection { } DataContract(Name = "response") public class MyCollection { DataMember XmlElement("entry") public List entry { get; set; } DataMember public int index { get; set; } } This has both XmlSerializer and DataContractSerializer attributes, but I had to lose the generics in the type we use for the response (hence the "closed MyResponse type).

If you are writing xml, I wonder whether xml serializer wouldn't be a better choice (it has more granular control over the names, etc). The problem, though, is that XmlSerializer isn't always the biggest fan of generics... Additionally - having tried a few options involving XmlArray / XmlArrayItem etc... it looks very hard to get it to the format you want... plus it isn't easy to guess what you mean by the T1 / T2 - but the following may come close: XmlRoot("response") public class MyResponse : MyCollection { } DataContract(Name = "response") public class MyCollection { DataMember XmlElement("entry") public List entry { get; set; } DataMember public int index { get; set; } } This has both XmlSerializer and DataContractSerializer attributes, but I had to lose the generics in the type we use for the response (hence the "closed" MyResponse type).

T1, T2 as in the items in "entry" ... let me try that out..... The problem is lists gets serialized as but I want – seanlinmt Jun 11 '09 at 13:54 The use of XmlElement on the list does exactly that. – Marc Gravell? Jun 11 '09 at 14:08 Ok... that doesn't seem to work out... I'm going to have a look at albahari.Com/nutshell/ch15.

Aspx again but 1 more day and I can't get a solution I guess I will use XmlSerializer for serializing to XML and ATOM. Or use surrogates... – seanlinmt Jun 11 '09 at 15:15 Hmm the XmlElement("entry") doesn't seem to make a difference. I haven't figured out how to incorporate the XmlRoot("response") though.

I need the generics. – seanlinmt Jun 11 '09 at 15:26 It does make a difference, but you have to use XmlSerializer – Marc Gravell? Jun 11 '09 at 17:16.

Based on this article, it seems that DataContractSerializer does not have support for customizing the resultant xml. danrigsby.com/blog/index.php/2008/03/07/... From the MS site there is an important note: 'The DataContractAttribute attribute should not be applied to classes that already implement the ISerializable interface or that are marked with the SerializableAttribute. An exception is thrown if you try to serialize an instance of such a type.' PS: Sorry I couldn't post the second link, but the engine does not allow post more than one link to new users.

Regards, Herberth.

1 Right. This is not what the DataContractSerializer is for. If you need detailed control over the XML, then you need the XmlSerializer.

– John Saunders Jul 15 '09 at 0:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions