反序列化为列表并再次绘制它们

时间:2018-10-16 00:29:34

标签: c# deserialization

我将一个类(形状类)序列化为自己的文件扩展名(.seal文件)。

 public void Serialization()
    {
            //Open the File Dialog for Saving
            SaveFileDialog save = new SaveFileDialog();
            //Custome File Extension
            save.Filter = "Seal Creator File (*.seal)|*.seal";
            save.DefaultExt = ".seal";
            save.AddExtension = true;

            if (save.ShowDialog() == DialogResult.OK)
            {
                //Serializers
                XmlSerializer ser = new XmlSerializer(typeof(List<Shape>));
                //XmlSerializer ser2 = new XmlSerializer(typeof(DataTable));
                FileStream file = File.Create(save.FileName);
                //Serialization process for the Class and DataSet
                ser.Serialize(file, draw._shapes);
                //ser2.Serialize(file, dataTable);
                file.Close();
                MessageBox.Show("Saved");
            }
    }

赞:

<?xml version="1.0"?>
<ArrayOfShape xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Shape>
    <size>
      <Width>480</Width>
      <Height>415.6922</Height>
    </size>
    <location>
      <X>10</X>
      <Y>12</Y>
    </location>
    <radius>0</radius>
    <type>triangle</type>
    <width>480</width>
    <height>415.6922</height>
    <strokeThickness>2</strokeThickness>
    <color />
    <userDefinedWidth>0</userDefinedWidth>
    <userDefinedHeight>0</userDefinedHeight>
    <userDefinedStroke>0</userDefinedStroke>
  </Shape>
</ArrayOfShape>

现在我要做的是将这些值再次存储到列表中,然后在图片框中重新绘制它们。

    public void DrawAllShapes(object sender, PaintEventArgs e)
    {
        foreach (Shape shape in _shapes)
        {
            switch (shape.type)
            {
                case Shape.ShapeType.rectangle:
                    shape.DrawRectangle(shape.color, shape.strokeThickness, shape.width , shape.height, e.Graphics, shape.radius);
                    break;
                case Shape.ShapeType.square:
                    shape.DrawSquare(shape.color, shape.strokeThickness, shape.width, shape.height, e.Graphics, shape.radius);
                    break;
                case Shape.ShapeType.circle:
                    shape.DrawCircle(shape.color, shape.strokeThickness, shape.width , shape.height , e.Graphics);
                    break;
                case Shape.ShapeType.ellipse:
                    shape.DrawEllipse(shape.color, shape.strokeThickness, shape.width, shape.height, e.Graphics);
                    break;
                case Shape.ShapeType.triangle:
                    shape.DrawTriangle(shape.color, shape.strokeThickness, shape.width, e.Graphics, shape.radius);
                    break;
            }
        }

反序列化:

public void Deserialize(StreamReader file)
    {
        try
        {
            List<Shape> shape_ = new List<Shape>();
            XmlSerializer ser = new XmlSerializer(shape_.GetType());
            List<Shape> shape = (List<Shape>)ser.Deserialize(file);
            shape_ = shape;
            shape_.ForEach(draw._shapes.Add);
            file.Close();
            pictureBox_Canvass.Invalidate();
            Debug.WriteLine(shape_.Count.ToString());
        }
        catch (Exception ex)
        {
            //Catching the Error
            String innerMessage = (ex.InnerException != null)? ex.InnerException.Message : "";
            Debug.WriteLine(innerMessage);
            MessageBox.Show("error");
        }
    }

编辑:添加了反序列化方法。现在看来我可以获取形状列表了,但是我仍然无法在pictureBox中重新绘制它们。

0 个答案:

没有答案