开发者

Serializing in C# not serializing a List when there is a DataTable on the side?

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


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.


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


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; }
}


For future reference to others visiting this issue... DataTables cannot be serialized. However, Datasets can, so you have to put your datatable in a dataset and then it can be serialized.


Try decorating your class as [DataContract] and then use the DataContractJsonSerializer class to serialize.

Check this link.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜