XmlSerializer: how to not serialize properties?

B

Beorne

I'm serializing a big class using XmlSerializer (I need xml
serialization cause compact framework). I've learn that all my fields
must be public. Ok.
Now it is completly useless to serialize my get/set properties, but I
haven't found an anttribute like
[NonSerialized]
working foer properties.
Any help?
 
T

Tom Porterfield

Beorne said:
I'm serializing a big class using XmlSerializer (I need xml
serialization cause compact framework). I've learn that all my fields
must be public. Ok.
Now it is completly useless to serialize my get/set properties, but I
haven't found an anttribute like
[NonSerialized]
working foer properties.


Why do you think all your fields must be public? When I serialize objects
to XML using he XmlSerializer, my fields are private and the serializer
works off of the public properties. The help documentation from MSDN
confirms this is the correct behavior - "The Serialize method converts the
public fields and read/write properties of an object into XML".

So leave your fields private, and have them accessible through public
properties, noting that the get and set must both be public.

That being said, if you want the XmlSerializer to not serialize a property
or field, add an XmlIgnoreAttribute to it.
 
M

Marc Gravell

The attribute is [XmlIgnore] - however I agree with the other
respondant; working with public fields is an ugly way to go...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top