XML Serialization using DataContractSerializer and XmlSerializer?

The first problem: since the types generated by "Add Web Reference" are decorated with Serializable by default, the serializable model used by the DataContractSerializer is that all fields (public or not) are serialized. If you decorate the type with DataContract and the members you want serialized (the properties) with DataMember A type can have the attributes for both serializers without problems, as shown below.

The first problem: since the types generated by "Add Web Reference" are decorated with Serializable by default, the serializable model used by the DataContractSerializer is that all fields (public or not) are serialized. If you decorate the type with DataContract and the members you want serialized (the properties) with DataMember. A type can have the attributes for both serializers without problems, as shown below.

Public class StackOverflow_7348240 { Serializable DataContract(Name = "myRoot", Namespace = "") XmlRoot(ElementName = "myRoot", Namespace = "") public class MyType { private string dataField; XmlElement(ElementName = "data") DataMember(Name = "data") public string Data { get { return this. DataField; } set { this. DataField = value; } } } public static void Test() { MyType obj = new MyType { Data = "hello world" }; MemoryStream ms = new MemoryStream(); new DataContractSerializer(obj.GetType()).

WriteObject(ms, obj); Console. WriteLine(Encoding. UTF8.

GetString(ms.ToArray())); ms. SetLength(0); new XmlSerializer(obj.GetType()). Serialize(ms, obj); Console.

WriteLine(Encoding. UTF8. GetString(ms.ToArray())); } } For the second issue, if the collection type is a member of another object, you can also add the appropriate attributes (this time the ones for XML serialization), namely XmlArray (to specify the collection name) and XmlArrayItem (to specify the item name), and you can have the same type serialized the same way, like in the example below.

Public class StackOverflow_7348240_b { DataContract(Name = "myRoot", Namespace = "") XmlRoot(ElementName = "myRoot", Namespace = "") public class MyType { DataMember(Name = "myArray") XmlArray(ElementName = "myArray") XmlArrayItem(ElementName = "url") public MyArray myArray; } CollectionDataContract(Name = "myArray", Namespace = "", ItemName = "url") XmlType(Namespace = "") XmlRoot(ElementName = "myArray", Namespace = "") public class MyArray : List { } public static void Test() { MyType obj = new MyType { myArray = new MyArray { "one", "two" } }; MemoryStream ms = new MemoryStream(); new DataContractSerializer(obj.GetType()). WriteObject(ms, obj); Console. WriteLine(Encoding.

UTF8. GetString(ms.ToArray())); ms. SetLength(0); new XmlSerializer(obj.GetType()).

Serialize(ms, obj); Console. WriteLine(Encoding. UTF8.

GetString(ms.ToArray())); } } Update I understood that you wanted to merge the types "by hand", but based on the comment this is not the case. In this case, if you want to use both Add Web Reference (AWR) and Add Service Reference (ASR), then you'll need to fall back to the common serializer, which is the XmlSerializer. AWR always uses the XmlSerializer (XS), while ASR can use both that one and the DataContractSerializer (DCS).

The DCS is the default one for ASR, but you can change it to use the other one. You have two options: Use svcutil. Exe (instead of ASR), and pass the /serializer:XmlSerializer command line After adding the service reference, open the Reference.

Svcmap file (you may need to check the "Show All Files" option for the project), then change the option from Auto to XmlSerializer, then select "Update Service Reference".

The problem here, those class are generated (it's web reference), so with your solution I'd have to re-declare all of them (because they are partial) and add this attributes, and there is 440 classes to redeclare. – remi bourgarel Sep 8 at 15:39 That's what I understood you were doing: "So I'm creating an object aggregating objects from both references. ".

In this case, since the Web Reference always uses the XmlSerializer, you can force the "Service Reference" to use that serializer as well - this way you'll get compatible objects. You can do that either using the svcutil command line (/serializer:XmlSerializer option) or, after adding the service reference, open the Reference. Svcmap file, and change the element from Auto to XmlSerializer.

– carlosfigueira Sep 8 at 19:52.

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