KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
To get more control over serialization, I have converted a class from [DataContract] to [Serializable] , implementing both GetObjectData and the special deserializing constructor. When I do this, the XML emitted now has type information applied to all elements. I don't want this superfluous information, and I'm wondering how to inform the serializer to not output it. Here's the sample code that uses [DataContract] : [DataContract(Namespace = "")] class Test { public Test() { } [DataMember] public Nullable<int> NullableNumber = 7; [DataMember] public int Number = 5; public static void Go() { var test = new Test(); var dcs = new DataContractSerializer(typeof(Test)); using (var s = new StreamWriter("test.xml")) { dcs.WriteObject(s.BaseStream, test); } } } This outputs the following XML (notice no type info on Nullable Number and Number--this is the desired output): <Test xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <NullableNumber>7</NullableNumber> <Number>5</Number> </Test> If I modify the above code as follows (adding [Serializable], : ISerializable, and the two serialization methods): [Serializable] class Test : ISerializable { public Test() { } public Nullable<int> NullableNumber = 7; public int Number = 5; public static void Go() { var test = new Test(); var dcs = new DataContractSerializer(typeof(Test)); using (var s = new StreamWriter("test.xml")) { dcs.WriteObject(s.BaseStream, test); } } public Test(SerializationInfo info, StreamingContext context) { NullableNumber = info.GetInt32("NullableNumber"); Number = info.GetInt32("Number"); } public void G
Tags (comma-separated)
Save Edits
Cancel