0

An unhandled exception of type 'System.InvalidCastException' occurred in sCreator.exe Additional information: Unable to cast object of type 'System.String' to type 'sCreator.Shape'.

Here's the code:

    public void Deseriaize(StreamReader file)
    {
        XmlSerializer ser = new XmlSerializer(typeof(string));
        Shape s = (Shape)ser.Deserialize(file);
        file.Close();
        MessageBox.Show(s.title);
    }

    private void btn_OpenProject_Click(object sender, EventArgs e)
    {
        StreamReader file = new StreamReader(@"C:\Users\pb8n0053\Documents\SerializationOverview.seal");
        Deseriaize(file);
    }

Shape Class

 [Serializable]
public class Shape
{
    //Properties
    public Draw draw;
    public String title;
    public float width { get; set; }
    public float height { get; set; }
    public float x { get; set; }
    public float y { get; set; }
    public static PointF center = new PointF(250f, 250f);
    public int strokeThickness { get; set; }
    public Color color { get; set; }
    public float userDefinedWidth { get; set; }
    public float userDefinedHeight { get; set; }
    public int userDefinedStroke { get; set; }
    public SizeF size;
    public PointF location;
    public float radius;
    public ShapeType type;
    public Status status;
    public enum ShapeType
    {
        rectangle, square, circle, ellipse, triangle, image
    }
    public enum Status
    {
        editing, start
    }
}
TerribleDog
  • 1,237
  • 1
  • 8
  • 31

1 Answers1

3

Your XmlSerializer is being created with typeof(string) as the argument to the constructor. This means that that serializer is intended to convert XML to and from a System.String. If you want it to convert your XML to and from your Shape type, then initialize it using that instead:

public void Deseriaize(StreamReader file)
{
    XmlSerializer ser = new XmlSerializer(typeof(Shape));
    Shape s = (Shape)ser.Deserialize(file);
    file.Close();
    MessageBox.Show(s.title);
}

Note that your serialization/deserialization cycle will probably fail or work incorrectly if you try to deserialize XML that was not created with XmlSerializer or if your Shape class does not properly implement ISerializable.

laptou
  • 6,389
  • 2
  • 28
  • 59
  • I get this error now: **An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There was an error reflecting type 'sCreator.Shape'.** – TerribleDog Oct 13 '18 at 01:46
  • @untargeted This means that there is a problem with your `Shape` class; it likely contains a member that `XmlSerializer` does not know how to handle. See [this question](https://stackoverflow.com/questions/60573/xmlserializer-there-was-an-error-reflecting-type) and make sure your type has a public parameterless constructor. – laptou Oct 13 '18 at 01:48
  • @untargeted I can't be sure without seeing the inner exception, but the problem is likely either with that member of type `Draw`, which may not be serializable. – laptou Oct 13 '18 at 01:50
  • [PointF is serializable](https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/PointF.cs), same with [SizeF](https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/SizeF.cs) – Ron Beyer Oct 13 '18 at 01:53
  • I marked 'draw' as non serializable. Now I get this error: was not expected. – TerribleDog Oct 13 '18 at 01:55
  • @untargeted Did you create the XML that you are trying to deserialize using an `XmlSerializer` with `typeof(Shape)` as its constructor argument? – laptou Oct 13 '18 at 01:56
  • As a string. If I serialize it as type of Shape. It would produce this error, **There was an error generating the XML document.** @laptou – TerribleDog Oct 13 '18 at 01:59
  • Oh. I already fixed it. It was the serialization problem after all. thank to all of you. – TerribleDog Oct 13 '18 at 02:00
  • @RonBeyer `SerializableAttribute` does not affect XML serialization; only binary serialization. – Kenneth K. Oct 13 '18 at 02:04