DataContractSerializer - 问题
我在数据契约序列化器方面遇到了一个非常愚蠢的问题。它拒绝工作。我只是想将一个对象序列化为 XmlDocument,但是我似乎遇到了困难。
以下是我希望序列化的数据契约:
[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; }
}
以及执行序列化的方法;
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;
}
每当我调用序列化方法时,内存流总是空的。我也尝试过字符串生成器,只是为了看看是否有任何结果。
如果我使用 XmlSerializer 这可以工作,但是我想了解为什么上面的代码不起作用?为什么序列化器总是空的?
感谢您的帮助! TM
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 XmlSerializer 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我用来序列化对象的代码,它似乎对我有用。另外,我认为 DataContract 属性不是必需的,我仅在一个地方使用属性来忽略成员。
我看到的差异是我直接写入 MemoryStream,并将 Position 设置为
0,而不是调用 Seek()。
如果需要,可以在实例上使用 XmlDocument.LoadXml 将生成的字符串加载到 XmlDocument 中。
This is the code I use to serialize my objects and it seems to work for me. Also I think the DataContract attributes are not necessary, I'm only using an attribute in one place to ignore a member.
The differences I see is that I'm writing directly to the MemoryStream, and setting the Position to
0, rather than calling Seek().
You could load the resulting string into an XmlDocument if you wanted to, using XmlDocument.LoadXml on the instance.
问题在于以下行,因为 XmlWriter 尚未将其内容刷新到内存流:
尝试改为使用:
The problem lies in the following line because the XmlWriter hasn't flushed it's contents to the memory stream yet:
Try instead using: