6

How do you specify a NonSerialized field with public accessors for XML Serialization?

[NonSerialized]
public String _fooBar;
//Declaring the property here will serialize the _fooBar field
public String FooBar
{
    get { return _fooBar; }
    set { _fooBar = value; }
}
MPelletier
  • 16,256
  • 15
  • 86
  • 137

1 Answers1

17

Properties don't get serialized by BinaryFormatter, only fields. The [NonSerialized] attribute has no meaning for XML serialization. Use [XmlIgnore] instead.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536