Yusufozgen
New Member
I have read many posts regarding deserialization of nullable fields but have not run across the following scenario:[*]Serialize an object with a nullable field that contains a value ("nil" attribute is not added to the node because it contains a value).[*]Remove the value from the nullable field in the xml (this happens via client-side processing).[*]Deserialize the xml.Step 3 throws an error because the serializer does not treat the empty value of the nullable field as a null value (because "nil=true" is not specified). It instead tries to convert the value to the field's data type (ex: Guid), which fails resulting in an error message that varies depending on the field's data type. In the case of a Guid the error message is:\[code\] System.InvalidOperationException: There is an error in XML document ([line number], [column number]). ---> System.FormatException: Unrecognized Guid format.\[/code\]I should note that the serialization / deserialization methods we use are framework methods that use generics.I'm looking for an elegant and generic solution. The only feasible, generic solution I can think of is the following:[*]Convert the xml to an XDocument.[*]Use (less than desired) reflection to get all of the properties of the object that are reference types.[*]Add "nil=true" attribute to all nodes whose name is found in the list from #2 and has an empty value.[*]Use recursion to process each reference type in #2.Note: Simply adding "nil=true" to all nodes that have an empty value will not work because the serializer will throw an error for value types that cannot be null.[Edit] Code examples:Sample data class\[code\] public class DummyData { public Guid? NullableGuid { get; set; } }\[/code\]Xml sent to client\[code\] <DummyData> <NullableGuid>052ec82c-7322-4745-9ac1-20cc4e0f142d</NullableGuid> </DummyData>\[/code\]Xml returned from client (error)\[code\] <DummyData> <NullableGuid></NullableGuid> </DummyData>\[/code\]Xml returned from client (desired result)\[code\] <DummyData> <NullableGuid p2:nil="true" xmlns2="http://www.w3.org/2001/XMLSchema-instance"></NullableGuid> </DummyData>\[/code\]