hi,

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);
w.Close();
}

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>();
_MyStringList.Add
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.

thanks.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.