如何序列化在另一个类中声明的一个类的列表?

时间:2018-10-23 01:22:05

标签: c# serialization

我有一个draw类,并且在这里声明了

    public List<Layers> layers = new List<Layers>();

作为我的图层类的列表。现在我要做的是将Draw Class序列化为Xml,但是它给出了一个反映类型图层的错误

public class Draw
{
    [XmlIgnore]
    public Shape s = new Shape();
    [XmlIgnore]
    public ImageClass img = new ImageClass();
    public List<Layers> layers = new List<Layers>();
}

层级

public class Layers
{
    public ImageClass _image { get; set; }
    public Shape _shape { get; set; }
    public enum Type { Rectangle, Square, Circle, Ellipse, Triangle, Image }
    public Layers.Type type { get; set; }
    public float width { get; set; }
    public float height { get; set; }
    public PointF location { get; set;}
    public int radius { get; set; }
    public int strokeThickness { get; set; }
    public Color color { get; set; }
    public Bitmap image { get; set; }
    public RectangleF rect { get; set; }
    public PointF centerSerializable { get { return Shape.center; } set { Shape.center = value; } }
}

序列化方法

public void Serialization()
    {
        try
        {
            XmlSerializer ser = new XmlSerializer(typeof(Draw)); //Serializer
            SaveFileDialog save = new SaveFileDialog();           //Creates and Opens a SaveFileDialog
            save.Filter = "Seal Creator File (*.sealFile)|*.sealFile";     //Creates a filter fir saving the Project File
            save.DefaultExt = ".seal";
            save.AddExtension = true;

            if (save.ShowDialog() == DialogResult.OK)
            {
                //Serialization process for the Class
                FileStream file = File.Create(save.FileName);
                ser.Serialize(file, draw);
                file.Close();
                MessageBox.Show("Saved"); //Saving Confirmation
            }
        }
        catch (Exception ex)
        {
            //Catching the Error
            String innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : "";
            Debug.WriteLine(innerMessage);
        }
    }

0 个答案:

没有答案