I have some public members I don't want to be serialized, was wondering if there is an attribute for it?
Asked
Active
Viewed 2,180 times
5 Answers
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
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