CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Ranjan Sakalley


Suppress serialization of a public property

A post on one of the microsoft groups prompted some research, I was bent upon finding out a solution one way or the other.


[Serializable]
public class Tested
{
       
        private string _value;
        public string Value
        {
                get
                {
                        return this._value;
                }
                set
                {
                        this._value = value;
                }
        }
}

 

When you serialize the class above, after setting the property Value to, say "test" using the default XML serializer that comes with the BCL (XMLSerializer instance) you will get something like this as an output -

<?xml version="1.0" encoding="IBM437"?>
<Tested xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-nstance">
  <Value>test</Value>
</Tested>

What will you do to make the default serializer ignore a public property? Lets start with a public member variable. Mark it as [NonSerialized] ofcourse. So if the class were something like this

[Serializable]
public class Tested
{
        [NonSerialized]
        public string Value;
       
}

and you serialized an instance of this class using XMLSerializer, the NonSerialized attribute is totally ignored, and you get

<?xml version="1.0" encoding="IBM437"?>
<Tested xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-nstance">
  <Value>test</Value>
</Tested>

So the XMLFormatter is not a solution at all. Also, you cannot mark a property with NonSerialized, as this attribute applies to fields only

Next, use a SoapFormatter and Serialize this instance, sending the instance as a graph you get


<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Tested id="ref-1" xmlns:a1="
http://schemas...">
</a1:Tested>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Value” is not serialized here, which is good for me and I can move back to my original problem, the one where I want the serializer/formatter to ignore a public property.

[Serializable]
public class Tested
{
        [NonSerialized]
        private string _value;
        public string Value
        {
                get
                {
                        return this._value;
                }
                set
                {
                        this._value = value;
                }
        }
}

Worked

Lessons learnt
XMLSerializer
        Serializes public properties and fields, doesn't matter if you mark them as NonSerializable or not.
Also, I must note that properties are serialized; even thought they are actually methods internally, the XMLSerializer calls them.

SOAPFormatter
        Serializes all fields (no properties), and respects NonSerializable.

 

Is this differing behaviour by design, or am I missing some configuration setting

Whatever be the case, I would still advocate implementing ISerializable, and writing your own serialization/deserialization code.


Published Jan 28 2005, 05:37 AM by rsakalley
Filed under:

Comments

Ranjan said:

Thanks for the pointer. I ignored that part totally
# January 28, 2005 1:33 AM

Ranjan said:

was more inclined towards SoapFormatter because of web services and remoting usages
# January 28, 2005 1:38 AM

Geoff Appleby said:

Yup, the formatters and the serializers are completely different beasts.
You need XMLIgnore when using the xmlserializer. Also, if you write classes that are going to be serialized, it's always helpful to add a DefaultValueAttribute to all properties that you expect to be serialized. anything that matches the default value will not be serialized at all, because it's expected that a default instance of your class when deserialized will already have the default value set. For the xmlserializer you can also do some playing with the IXMLSerializable interface, which lets you take over the entire serialization process - you output the xml you want to serialize yourself using an xmlwriter. then there's the ISerializable interface, which is the same sort of deal, but for the formatters.

The current .net 1.1 doco says that IXMLSerializable should not be used, but it's publicly documented and usable in the 2.0 framework (and works exactly the same way in 1.1)

Damn annoying hey?
# January 28, 2005 1:39 AM

Ranjan said:

Geoff,
This is exactly why I have never touched IXMLSerializable, will try it out with 2.0 though. About defaults, yes, and that is how I was able to figure out how to interoperate between nullable value types in 2.0 and value types in 1.1.
And yes, its hard to not hate the Not(Yet)ImplementedException.
Thanks,
Ranjan
# January 28, 2005 2:08 AM

Albert said:

You can suppress the serialization of specific properties by using the XmlIgnore attribute. So your code would become:

public class Tested
{
private string _value;
[XmlIgnore()]
public string Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
}

Thats it.
# July 25, 2005 5:39 PM

NeVaL said:

http://www.lesbian-reality.beibi.info ^^^ http://www.lesbo-pornoa.beibi.info ^^^ http://www.avril-naken-porno.biseksuell.info ^^^ http://www.image-naturlig-toyte.biseksuell.info ^^^ http://www.avril-naken-porno.erotiska.info ^^^ http://www.image-naturlig-toyte.erotiska.info ^^^ http://www.filmer-sms.fitta69.info ^^^ http://www.piss-hunters.fitta69.info ^^^ http://www.exotisk-fotsex-bild.fotsex.info ^^^ http://www.brudar-filmer-jpg.fotsex.info ^^^ http://www.mummo-vittu-kela.isomuna.info ^^^ http://www.seksi-girl-ilmasto.isomuna.info ^^^ http://www.ilmasto-milf-lesbian.laukeaminen.info ^^^ http://www.porno-kuvia-pattaya.laukeaminen.info ^^^ http://www.rype-vat.rype.info ^^^ http://www.pul-vaate-fitte.rype.info ^^^ http://www.rav-striptease-image.sadsprut.info ^^^ http://www.film-ung-mann.sadsprut.info ^^^ http://www.kela-cunt-wwwkosovarja.tytsy.info ^^^ http://www.otos-lukuunottamatta-imaisu.tytsy.info ^^^ http://www.anal-lovers-pics.18analsex.com ^^^ http://www.anal-girl-machine.18analsex.com ^^^ http://www.inculata-anale-clip.pazzesesso.com ^^^ http://www.sex-bdsm.pazzesesso.com ^^^ http://www.dvd-biondo-tevere.figanere.com ^^^ http://www.emisoras-peruanas-porno.figanere.com ^^^ http://www.cutie-teenager-foto.inculatexxx.com ^^^ http://www.liceali-troia-immagine.inculatexxx.com ^^^ http://www.hot-holiday-una-calda-vacanza.prostitutaculo.com ^^^ http://www.gratis-sederi-nudi.prostitutaculo.com ^^^ http://www.duepi-racconto-gif.lesbicastrip.com ^^^ http://www.porno-vibratore-ditalino.lesbicastrip.com ^^^ http://www.dvd-foto-novelas.007sexogratis.com ^^^ http://www.clip-ericka-velez.007sexogratis.com ^^^ http://www.gals-wwwvajinascom.3sexogratis.com ^^^ http://www.dvd-jovensitas-cojiendo.3sexogratis.com ^^^ http://www.film-peluda-mujer.analsexogratis.com ^^^ http://www.lolitas-jovensitas-video.analsexogratis.com ^^^ http://www.nombre-nina-fotos.cam-sexo-gratis.com ^^^ http://www.xxx-maduras-asiaticas.cam-sexo-gratis.com ^^^

# December 28, 2006 6:08 AM

Smułko said:

http://vacanza-moto-sicilia.jvh3ddn24oi.info/

http://sulla.7djyd626ukf.info/

http://kurzevie.u3yomyk50cp.info/

http://desire-infermiera-azione.nmlnrjuk87j.info/

http://zurigo-albergo.hgmtpx1eyo1.info/

http://ragazza-nuda.3wcekxwg3md.info/

http://www.ef9iyw6m643.info/145010756/

http://fuoriclasse-lesbiche-inculate.7djyd626ukf.info/

http://derisive-amatoriali-ubriache.5rbqrq3fqgi.info/

http://accendino-gas.onocffkylv1.info/

http://usy3jlnzak4.u3yomyk50cp.info/

http://vacanza-benessere-italia.onocffkylv1.info/

http://ucs7jl2.u3yomyk50cp.info/

http://149058960.zp9as3i9llc.info/

http://osare-fighette-strip.u4re8o6n1qf.info/

http://urbanistica-appalto.jht3k7963m7.info/

http://caldissimo-gay.dewo9m907by.info/http://www.ducis.nom.es/discoteca-hollywood-milano.html

http://144946552.bibbo.com.es/

http://www.quad.org.es/agente-di-polizia-doppio-penetrazione-in-cucina.html

http://www.dmdaa.org.es/igiene-lavoro.html

http://carino-pene.vtya.com.es/

http://www.subli.org.es/prostitute-nere.html

http://government-jobs.fescu.nom.es/

http://www.fescu.nom.es/timido-fighette-strip.html

http://ultneh21.psaro.com.es/

http://www.socl.nom.es/tipografia-mare.html

http://www.pipes.org.es/finanziaria-banca.html

http://amatoriali-masturbate-nella-stanza.worri.org.es/

http://www.fapas.org.es/assurdo-agente-di-polizia-orale-fotti.html

http://uuoep6c6e.psaro.com.es/

http://vrvt4ngubpz.cemaf.org.es/

http://144928116.bibbo.com.es/

http://bonny-agente-di-polizia-azione.vidot.com.es/

http://www.fapas.org.es/senssex-asiatiche-merda.html

http://www.dindl.nom.es/londra-trasporti.html

http://www.miclo.com.es/body-building-supplements.html

http://www.cranv.com.es/ge2ga47m.html

# January 24, 2007 1:29 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications