NonSerialized

G

Guest

Hi everyone

I just want to ask why this code below adds the
variables i,v to the serialization too ? I have made them as NonSerialized

I'm using XmlSerializer and TextWriter classes to create xml file
with an instance of class x

any suggestions

than

// Code ..........................

[Serializable()
public class

public x(

/
// TODO: Add constructor logic her
/


[NonSerialized()]
public int v=2000

[NonSerialized()]
private int i=1000

private int i1;

public int i

get
return i

set
i=value



public int ii

get
return i1

set
i1=value



}
 
F

Frans Bouma [C# MVP]

Oren said:
Hi everyone.

I just want to ask why this code below adds the
variables i,v to the serialization too ? I have made them as NonSerialized.

I'm using XmlSerializer and TextWriter classes to create xml file
with an instance of class x.

any suggestions ?

thanx

// Code ...........................

[Serializable()]
public class x
{
public x()
{
//
// TODO: Add constructor logic here
//
}

[NonSerialized()]
public int v=2000;

[NonSerialized()]
private int i=1000;

NonSerialized and Serializable are for SOAP/Binary formatter' serialization.
XmlSerializer uses IXmlSerializable as interface. You should apply the
XmlIgnore attribute to the properties ii and ii1 to have them not end up in
the XML.

Frans.
 
M

Miha Markic [MVP C#]

Hi Oren,

Try using XmlIgnoreAttribute on the property or field that shouldn't be
serialized.
Note that Xml* attributes control the XML serialization.
See
Attributes That Control XML Serialization
..net help topic.
 

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