NonSerialized dind't work

I am serializing a class, but I cannot exclude any field in my class.

[Serializable] public class DicData { private GDicJson DeserializedGDicJson = new GDicJson(); public UOCDicData BuiltDicData; [NonSerialized] public string CacheName = ""; } 

in my outcome, the public CacheName field was not included in my * .xml deserialized output, but it was included in the .xml file.

here routine is serialized.

 XmlSerializer myXml = new XmlSerializer(typeof(DicData), "test"); myXml.Serialize(myFile, this); //note:a serializing perform in method of himself. 
+4
source share
1 answer

For XmlSerializer you want

 [XmlIgnore] 

Also note that in this case [Serializable] not required.

As a final note: public fields are not encouraged; almos properties are always preferred. Adding {get;set;} would go a long way ...

+16
source

All Articles