Home All Groups Group Topic Archive Search About

why I can serialize a class without using [Serializable]?

Author
19 Nov 2004 10:37 PM
ALI-R
I have written a class in C# and I have serilized into a file .I read
somewhere that if you want a class to be serializble you must add an
attribute called [Serializable] before the class and within the namespace,I
didn't do that but I could serialize it using the following code:
try

{

XmlSerializer s = new XmlSerializer( typeof( EnviroVars) );

s.Serialize( fs, enviromental_Vars );

}

catch(Exception ee)

{

MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source);

}

finally

{

fs.Close();

}



thanks for your help

Author
23 Nov 2004 5:24 AM
Dino Chiesa [Microsoft]
[Serializable] is for runtime serialization.

Your code uses XML Serialization.
XML and Runtime serialization are different mechanisms, and they behave
differently , and are governed  by different attributes.

The XML Serializer ignores the [Serializable] attribute.

-Dino


Show quote
"ALI-R" <new***@microsoft.com> wrote in message
news:uhNRdjozEHA.1652@TK2MSFTNGP11.phx.gbl...
>I have written a class in C# and I have serilized into a file .I read
> somewhere that if you want a class to be serializble you must add an
> attribute called [Serializable] before the class and within the
> namespace,I
> didn't do that but I could serialize it using the following code:
> try
>
> {
>
> XmlSerializer s = new XmlSerializer( typeof( EnviroVars) );
>
> s.Serialize( fs, enviromental_Vars );
>
> }
>
> catch(Exception ee)
>
> {
>
> MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source);
>
> }
>
> finally
>
> {
>
> fs.Close();
>
> }
>
>
>
> thanks for your help
>
>
Author
26 Nov 2004 9:50 PM
ALI-R
thanks for the info.so interesting
Show quote
"Dino Chiesa [Microsoft]" <din***@online.microsoft.com> wrote in message
news:%23y723yR0EHA.1260@TK2MSFTNGP12.phx.gbl...
> [Serializable] is for runtime serialization.
>
> Your code uses XML Serialization.
> XML and Runtime serialization are different mechanisms, and they behave
> differently , and are governed  by different attributes.
>
> The XML Serializer ignores the [Serializable] attribute.
>
> -Dino
>
>
> "ALI-R" <new***@microsoft.com> wrote in message
> news:uhNRdjozEHA.1652@TK2MSFTNGP11.phx.gbl...
> >I have written a class in C# and I have serilized into a file .I read
> > somewhere that if you want a class to be serializble you must add an
> > attribute called [Serializable] before the class and within the
> > namespace,I
> > didn't do that but I could serialize it using the following code:
> > try
> >
> > {
> >
> > XmlSerializer s = new XmlSerializer( typeof( EnviroVars) );
> >
> > s.Serialize( fs, enviromental_Vars );
> >
> > }
> >
> > catch(Exception ee)
> >
> > {
> >
> > MessageBox.Show(ee.Message + "**" + ee.StackTrace + "**" + ee.Source);
> >
> > }
> >
> > finally
> >
> > {
> >
> > fs.Close();
> >
> > }
> >
> >
> >
> > thanks for your help
> >
> >
>
>

AddThis Social Bookmark Button