非序列化和Xml.Serialization.XmlIgnore之间的区别? [英] Difference between NonSerialized and Xml.Serialization.XmlIgnore?

查看:851
本文介绍了非序列化和Xml.Serialization.XmlIgnore之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在序列化/反序列化从XML类,但也有我们要排除类的属性和字段。

System.NonSerialized System.Xml.Serialization.XmlIgnore 属性似乎做的工作,但什么是它们之间的区别是什么?似乎我们可以使用 XmlIgnore 在任一属性或类的字段。但非序列化只能用在各个领域。有什么理由不使用XmlIgnore在任何情况下,如果有什么非序列化属性的目的,为什么在使用不同?


这是允许的:

 < System.NonSerialized()> _
公共富作为字符串
 


这是允许的:

 < System.Xml.Serialization.XmlIgnore()> _
公共富作为字符串
 


这是允许的:

 < System.Xml.Serialization.XmlIgnore()> _
公共属性栏()作为字符串
    得到
        返回_Bar
    最终获取
    设置(BYVAL值作为字符串)
        _Bar =价值
    结束设定
高端物业
 


但是,这是不允许的:

 < System.NonSerialized()> _
公共属性栏()作为字符串
    得到
        返回_Bar
    最终获取
    设置(BYVAL值作为字符串)
        _Bar =价值
    结束设定
高端物业
 

解决方案

非序列化应用到更多类型的序列比XML的。你可以使用非序列化,如果你被序列化到二进制或SOAP和XmlIgnore如果你使用一个XmlSerializer严格序列化到XML。见的<一个注释部分href="http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx">NonSerializedAttribute班MSDN。

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.

这篇关于非序列化和Xml.Serialization.XmlIgnore之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆