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