C#. NET how to Not serialize fields, properties
When we use public properties and public fields, we are able to serialize them smoothly.
View Sourceprint?01.[Serializable]02.public class MyClass03.{04. public int ID;05. 06. public string Address;07. 08. private int _age;09. 10. public string Name { get; set; }11. 12. public int Age13. {14. get { return _age; }15. set { _age = value; }16. }17.}The result of XML serialization into a file is like this:
Json
Bin because the bin file is invisible to the naked eye, so it is represented by deserialization
But there will always be fields or properties that you don't want to save as files.
JSON and XML do not want to be serialized "Properties" and "fields" using the following attribute:
[System.Xml.Serialization.XmlIgnore] [System.Web.Script.Serialization.ScriptIgnore] BinaryFormatter If you do not want to be serialized "attribute" Just use the following in the corresponding "field": [NonSerialized] Look at an example:
View Sourceprint?01.[Serializable]02.public class MyClass03.{04. [NonSerialized]05. public int ID;06. 07. public string Address;08. 09. [NonSerialized]10. private int _age;11. 12. public string Name { get; set; }13. 14. [System.Xml.Serialization.XmlIgnore]15. [System.Web.Script.Serialization.ScriptIgnore]16. public int Age17. {18. get { return _age; }19. set { _age = value; }20. }21.}XML serialization, ignoring the Age property
JSON serialization, ignoring the Age property
BinaryFormatter serialization ignores the Age property and ID field
Go C#. NET how to Not serialize fields, properties