5

I have some public members I don't want to be serialized, was wondering if there is an attribute for it?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

5 Answers5

7

You are looking for XmlIgnore

Ryan Rinaldi
  • 4,119
  • 2
  • 22
  • 22
5

Use XmlIgnoreAttribute to do it statically, as others advised you.

You can also do it dynamically. Suppose you have a serializable property Name. Then the following:

[XmlIgnore]
public bool NameSpecified {
    get {
        // your logic here
    }
}

If this special property getter returns true, Name will be included in the output XML document, otherwise, it won't be included. NameSpecified itself is marked with [XmlIgnore] because you obviously don't want to include it.

azheglov
  • 5,475
  • 1
  • 22
  • 29
4

XmlIgnore will do the trick.

David Brown
  • 35,411
  • 11
  • 83
  • 132
2

Use XmlIgnore attribute

Vitaliy Liptchinsky
  • 5,221
  • 2
  • 18
  • 25
0

Following on from Azheglov's comment regarding the "Specified" suffix, the DefaultValue attribute is also considered when serializing, and a value won't be serialized at all if it is the default.

Markie
  • 9
  • 1