views:

118

answers:

5

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

+4  A: 

XmlIgnore will do the trick.

David Brown
+6  A: 

You are looking for XmlIgnore

Ryan Rinaldi
+2  A: 

Use XmlIgnore attribute

Vitaliy Liptchinsky
+2  A: 

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
A: 

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