I have a application that needs to serialize a custom object and send it to a windows service, the custom object contains 2 lists of custom objects and a dictionary of int, string. When i try to serialize the object I am getting the error message:
There was an error generating the XML document.
I’ve googled around and found that this is generally due to one of the datatypes not being setup for serialization correctly. So I’ve gone through and verified the serialization on all the custom classes and as far as I can tell it is setup correctly.
My question now is, are lists and dictionaries serializable by default or does something need to be done in order to serialize them? Or, is there a better way to serialize collections of custom objects to be passed between executables?
Edits:
Main Custom Class:
[Serializable]
class MoveInInfoRequest : ServerRequestData
{ }
[Serializable]
[XmlInclude(typeof(GetUnitTypesResponseData)), XmlInclude(typeof(VendorObj.RequiredFields)),
XmlInclude(typeof(VendorObj.InsuranceChoice)), XmlInclude(typeof(VendorObj.ProrateSettings))]
public class MoveInInfoResponse : ServerResponseData
{
public GetUnitTypesResponseData UnitTypesInfo
{ get; set; }
public List<VendorObj.RequiredFields> RequiredFields
{ get; set; }
public Dictionary<int, String> RentalPeriods
{ get; set; }
public List<VendorObj.InsuranceChoice> InsCoverageAmounts
{ get; set; }
public VendorObj.ProrateSettings ProrateOptions
{ get; set; }
}
Sampple Sub class: the other two classes are set up similar to this just much longer but they only use default datatypes.
<Serializable(), DataContract([Namespace]:="*companyNamespace*")> _
Public Class InsuranceChoice
Public Sub New()
End Sub
<DataMember()> _
Public InsuranceChoiceID As Integer
<DataMember()> _
Public CoverageDescription As String
<DataMember()> _
Public Premium As Decimal
<DataMember()> _
Public ActualCoverageAmount As Decimal
End Class
It depends on what you are trying to serialize them with. In particular, Dictionary objects are not serializable if you are using
XmlSerializer, though they are if you are usingDataContractSerializer. You should be fine to serialize a List.If you would like an alternative to Xml serialization, you could serialize to JSON using Json.Net.
References:
Serialize Class containing Dictionary member
Serializing .NET dictionary
Why doesn't XmlSerializer support Dictionary?
http://theburningmonk.com/2010/05/net-tips-xml-serialize-or-deserialize-dictionary-in-csharp/