I am trying to Serialize a class in C# this works fine when there is no null value in the object. Following is the class
public class EnquiryResponseInfo
{
public string EnquiryId { get; set; }
public EnquiryViewModel Enquiry { get; set; }
}
When I supply the following value it works great.
EnquiryResponseInfo tt = new EnquiryResponseInfo()
{
EnquiryId = "xxx",
Enquiry = new EnquiryViewModel()
{
Name = "Test user",
Address = "Test Address"
}
}
But when Enquiry is null it does not Serialize. I have a condition where the Enquiry will be null but there will be value in the EnquiryId there.
Following is the method to Serialize the class.
public static string Serialize<T>(T toSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
Please help.