im using this code to serialize my class into xml:

public static void Save(Class1 myClass)
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(@"./myFile.cfg", Encoding.ASCII);
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(myClass.GetType());
x.Serialize(w, myClass);

my class has only 2 things to serialize. these are private member variables, which i provide access to via public properties:

/* private vars */
private List<string> _MyStringList = new List<string>();
private string _MyString;
/* public properties */
public string MyString
get { return _MyString; }
set { _MyString = value; }
public List<string> MyStringList
get { return _MyStringList; }
set { _MyStringList= value; }

both _MyStringList and _MyString are initialized in the constructor. but only _MyStringList gets serialized. why does the list of strings get seriaslized but not the string?

if i make _MyString (the actual private variable) public, then all works well. but i dont want public members vars.

if i set the private _MyString variable through the public MyString property *before* serializing, then this works too. i would just like to know why i simply cannot intiialize the private member in the constructor and then this value is serialized.


9 Years
Discussion Span
Last Post by unclepauly

i think i found the problem, by accident.

originally, the set accessors were commented out in the properties (although they are not in the code above). this causes the value not to be serialized. but when i included the set accessors, it worked fine.

still unsure as to why this though!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.