I have something that should be a VERY STRONG issue with the Serializer Data Contract. He refuses to work. I'm just trying to serialize an object in an XmlDocument, but I seem to be pushing the wall.
Here is the data I want to serialize:
[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 a method that performs serialization;
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 also tried the line builder, just to find out if something came out.
If I use XmlSerializer, this works, however, I would like to understand why the code above actually does not work? Why is the serializer always empty?
Thanks for any help! TM
source
share