XmlSerializer - 使用 URI 属性序列化对象时出错
我在序列化具有 Uri
属性的类时遇到问题。
System.InvalidOperationException was unhandled
Message=There was an error reflecting type 'Foo.Story'.
// ...
InnerException: System.InvalidOperationException
Message=There was an error reflecting property 'MyURI'.
我希望这个属性被序列化。有什么方法可以解决这个问题?我应该声明某种转换器并使用 URI 的字符串表示形式吗?
I'm having trouble serializing a class with a Uri
property.
System.InvalidOperationException was unhandled
Message=There was an error reflecting type 'Foo.Story'.
// ...
InnerException: System.InvalidOperationException
Message=There was an error reflecting property 'MyURI'.
I would like this property to be serialized. What is a way around this? Should I declare some sort of a converter, and use the string representation of the URI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Uri
类不能序列化为 XML,因为它没有默认构造函数,并且它的所有属性都是只读的。作为解决方法,您可以改为序列化字符串:The
Uri
class is not serializable to XML, because all it doesn't have a default constructor and all its properties are read-only. As a workaround, you can serialize a string instead:它必须是 XmlSerializer 吗?
DataContractSerializer
可以工作:这是一个很好的 文章 总结了差异
Does it have to be
XmlSerializer
?DataContractSerializer
would work:Here is a nice article that sums up the differences