Question : XML Serialization of Polygon

I need to serialize a Polygon element within a class and am stuck.  Any help?

Answer : XML Serialization of Polygon

here's the complete MyShape class for testing:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
public class MyShape : IXmlSerializable
    {
        public int _id;
        public string _name;
        public Polygon _polygon = null;

        public MyShape()
        {
            _polygon = new Polygon();
            _polygon.Stroke = System.Windows.Media.Brushes.Black;
            _polygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
            _polygon.StrokeThickness = 2;
            _polygon.HorizontalAlignment = HorizontalAlignment.Left;
        }

        public MyShape(int id, string name)
        {
            _id = id;
            _name = name;
            _polygon = new Polygon();
            _polygon.Stroke = System.Windows.Media.Brushes.Black;
            _polygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
            _polygon.StrokeThickness = 2;
            _polygon.HorizontalAlignment = HorizontalAlignment.Left;
            _polygon.VerticalAlignment = VerticalAlignment.Center;
            System.Windows.Point Point1 = new System.Windows.Point(1, 50);
            System.Windows.Point Point2 = new System.Windows.Point(10, 80);
            System.Windows.Point Point3 = new System.Windows.Point(50, 50);
            PointCollection myPointCollection = new PointCollection();
            myPointCollection.Add(Point1);
            myPointCollection.Add(Point2);
            myPointCollection.Add(Point3);
            _polygon.Points = myPointCollection;
        }

        #region IXmlSerializable Members

        public XmlSchema GetSchema()
        {
            return (null);
        }

        public void ReadXml(XmlReader reader)
        {
            reader.Read();
            XmlSerializer x = new XmlSerializer(typeof(string));
            _id = int.Parse(x.Deserialize(reader).ToString());
            _name = x.Deserialize(reader).ToString();
            _polygon.Points = new PointCollection(x.Deserialize(reader).ToString().Split(';').Select(n => Point.Parse(n)));
        }

        public void WriteXml(XmlWriter writer)
        {
            XmlSerializer x = new XmlSerializer(typeof(string));
            x.Serialize(writer, _id.ToString());
            x.Serialize(writer, _name);
            x.Serialize(writer, string.Join(";", _polygon.Points.Cast<Point>().Select(n => n.ToString()).ToArray()));
        }

        #endregion
    }
Random Solutions  
 
programming4us programming4us