Difference between NonSerialized and Xml.Serialization.XmlIgnore?(NonSerialized 和 Xml.Serialization.XmlIgnore 之间的区别?)
问题描述
我们正在从 XML 中序列化/反序列化一个类,但是我们想要排除该类中的一些属性和字段.
System.NonSerialized 和 System.Xml.Serialization.XmlIgnore 属性似乎可以完成这项工作,但它们之间有什么区别?看来我们可以在类的属性或字段上使用 XmlIgnore.但是 NonSerialized 只能用于字段.是否有任何理由不在每种情况下都使用 XmlIgnore,如果是,NonSerialized 属性的用途是什么以及为什么使用方式不同?
<小时>这是允许的:
<System.NonSerialized()>_公共 Foo 作为字符串
<小时>
这是允许的:
<System.Xml.Serialization.XmlIgnore()>_公共 Foo 作为字符串
<小时>
这是允许的:
<System.Xml.Serialization.XmlIgnore()>_公共属性 Bar() 作为字符串得到返回_Bar结束获取设置(ByVal 值作为字符串)_Bar = 值结束集结束属性
<小时>
但这是不允许的:
<System.NonSerialized()>_公共属性 Bar() 作为字符串得到返回_Bar结束获取设置(ByVal 值作为字符串)_Bar = 值结束集结束属性
NonSerialized 适用于比 XML 更多类型的序列化.如果要序列化为二进制或 SOAP,则使用 NonSerialized;如果使用 XmlSerializer 严格序列化为 XML,则使用 XmlIgnore.请参阅 MSDN 上 NonSerializedAttribute 类的备注部分.p>
We're serializing/deserializing a class from XML but there are properties and fields in the class which we want to exclude.
The System.NonSerialized and System.Xml.Serialization.XmlIgnore attributes seem to do the job but what's the difference between them? It seems we can use XmlIgnore on either properties or fields of the class. But NonSerialized can only be used on fields. Is there any reason not to use XmlIgnore in every case, and if so what's the purpose of the NonSerialized attribute and why the difference in usage?
This is allowed:
<System.NonSerialized()> _
Public Foo As String
This is allowed:
<System.Xml.Serialization.XmlIgnore()> _
Public Foo As String
This is allowed:
<System.Xml.Serialization.XmlIgnore()> _
Public Property Bar() As String
Get
Return _Bar
End Get
Set(ByVal value As String)
_Bar = value
End Set
End Property
But this is not allowed:
<System.NonSerialized()> _
Public Property Bar() As String
Get
Return _Bar
End Get
Set(ByVal value As String)
_Bar = value
End Set
End Property
NonSerialized applies to more types of serialization than XML. You would use NonSerialized if you were serializing to binary or SOAP, and XmlIgnore if you were strictly serializing to XML using an XmlSerializer. See the remarks section of the NonSerializedAttribute class at MSDN.
这篇关于NonSerialized 和 Xml.Serialization.XmlIgnore 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NonSerialized 和 Xml.Serialization.XmlIgnore 之间的区别?


- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01