tags:

views:

54

answers:

4

Here's my code:

[Serializable()]
public class Project
{
    private List<string> _Kinds = new List<string>();
    public DataTable ExtractedElementsTable;

    public Project()
    {
        ExtractedElementsTable = new DataTable();
        ExtractedElementsTable.TableName = "Output";
    }

    public List<string> Kinds
    {
        get { return _Kinds; }
        set { _Kinds = value; }
    }
}

When, after adding some stuff to the List<string> _Kinds, I try to serialize the whole Poject, and then deserialize it, the _Kinds list is empty. But if I comment out all the three lines where ExtractedElementsTable is referenced, it works ok. Here's is my serializing and deserializing code (note the currentProject.Kinds.Add("hi"); line in the serializing code. currentProject is just an instance of Project.

    private void openButton_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            Stream stream = File.Open(openFileDialog1.FileName, FileMode.Open);
            XmlSerializer xmlFormatter = new XmlSerializer(currentProject.GetType());
            currentProject = (Project)xmlFormatter.Deserialize(stream);
            stream.Close();
        }
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            currentProject.Kinds.Add("hi");
            Stream stream = File.Open(saveFileDialog1.FileName, FileMode.Create);
            XmlSerializer xmlFormatter = new XmlSerializer(currentProject.GetType());
            xmlFormatter.Serialize(stream, currentProject);
            stream.Close();
        }
    }
+1  A: 

XML serialization does not serialize private properties and fields. Either make _Kind public and remove getter and setter, or use binary serialization.

fuwaneko
the Kinds property does serialize. It is the deserialization which is the issue.
gbogumil
Just test it for yourself, if you don't believe MSDN :)
fuwaneko
I did test it. The result was that the public property serialized the list and the public field also serialized. The private field did not serialize. Upon deserialization though the public property did not deserialize. The public field did however (checked the TableName property). After changing DataTable to DataSet both worked as expected.
gbogumil
+1  A: 

Change the DataTable field to a DataSet field.

I ran quite a few test. The Kinds property gets serialized with all the items. But does not deserialize properly. However, I changed the DataTable field to a DataSet and it all worked fine.

gbogumil
That worked. Thanks.
jsoldi
you are welcome.
gbogumil
I just ran across an article from MS that stated XML serialization of DataTable and DataRow do not work. Changing to a DataSet was their answer as well.
gbogumil
A: 

Try this on for size. I was able to get valid serialization and deserialization with Project designed like so:

[Serializable()]
public class Project
{
    private List<string> _Kinds = new List<string>();

    public Project()
    {
        ExtractedElementsTable = new DataTable();
        ExtractedElementsTable.TableName = "Output";
    }

    public List<string> Kinds
    {
        get { return _Kinds; }
        set { _Kinds = value; }
    }

    [XmlElement("ExtractedElements")]
    public string ExtractedElementsXml
    {
        get
        {
            using (var writer = new StringWriter())
            {
                this.ExtractedElementsTable.WriteXml(writer);
                return writer.ToString();
            }
        }

        set
        {
            using (var reader = new StringReader(value))
            {
                this.ExtractedElementsTable.ReadXml(reader);
            }
        }
    }

    [XmlIgnore]
    public DataTable ExtractedElementsTable { get; set; }
}
kbrimington
A: 

Try decorating your class as [DataContract] and then use DataContractJsonSerializer class to serialize. Check this link: http://dotnetarchitect.wordpress.com/2009/03/09/tip-c-automatic-properties-k_backingfield-and-json/

BVT