Hi guys,
I'm having what must be a VERY SILLY issue with the Data Contract Serializer. It refuses to work. I'm just trying to serialize an object into an XmlDocument, however I seem to be hitting a wall.
Here are the datacontracts I wish to serialise:
[DataContract(Namespace="urn://test", Name = "ServiceFault1")]
public class ServiceFault
{
[DataMember()]
public int hello { get; set; }
[DataMember()]
public List<Error> Errors {get; set;}
}
[DataContract(Namespace = "urn://test", Name = "Error1")]
public class Error
{
[DataMember()]
public string ErrorCategoryCode { get; set; }
[DataMember()]
public string LocalErrorCode { get; set; }
[DataMember()]
public string Description { get; set; }
}
And the method that does the serializing;
public static XmlDocument Serialize(ServiceFault toSer)
{
DataContractSerializer ser = new DataContractSerializer(toSer.GetType());
MemoryStream mem = new MemoryStream();
ser.WriteObject(XmlWriter.Create(mem), toSer);
XmlDocument tmp = new XmlDocument();
mem.Seek(0, SeekOrigin.Begin);
tmp.Load(mem);
return tmp;
}
Whenever I call the serialize method, the memory stream is always empty. I've tried a string builder as well, just to see if anything was coming out.
If I use the XmlSerailizer this works, however I'd like to understand why on earth the code above does not work? Why is the serializer always empty?
Thanks for any help! TM