Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml.Serialization;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create some objects
- List<DrawingObject> dObj = new List<DrawingObject>();
- dObj.Add(new Line(100, 100));
- dObj.Add(new Circle(50,10,10));
- dObj.Add(new Square(10,10,20,20));
- dObj.Add(new DrawingObject());
- Serialize(dObj);
- List<DrawingObject> deserializedList = new List<DrawingObject>();
- // deserialize to a generic list
- deserializedList = Deserialize<List<DrawingObject>>();
- // display the results
- foreach (DrawingObject DrawingObject in deserializedList)
- {
- switch (DrawingObject.GetType().Name)
- {
- case "Line":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Line line = (Line)DrawingObject;
- break;
- case "Circle":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Circle circle = (Circle)DrawingObject;
- break;
- case "Square":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Square square = (Square)DrawingObject;
- break;
- default:
- Console.WriteLine (string.Format("I'm just a {0}", DrawingObject.Draw()) );
- break;
- }
- }
- Console.ReadLine();
- }
- private static void Serialize<T>(T data)
- {
- // Use a file stream here.
- using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
- {
- // Construct a SoapFormatter and use it
- // to serialize the data to the stream.
- XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
- try
- {
- // Serialize EmployeeList to the file stream
- SerializerObj.Serialize(WriteFileStream, data);
- }
- catch (Exception ex)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
- }
- }
- }
- private static T Deserialize<T>() where T : new()
- {
- //List<Employee> EmployeeList2 = new List<Employee>();
- // Create an instance of T
- T ReturnListOfT = CreateInstance<T>();
- // Create a new file stream for reading the XML file
- using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- // Construct a XmlSerializer and use it
- // to serialize the data from the stream.
- XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
- try
- {
- // Deserialize the hashtable from the file
- ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
- }
- catch (Exception ex)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
- }
- }
- // return the Deserialized data.
- return ReturnListOfT;
- }
- // function to create instance of T
- public static T CreateInstance<T>() where T : new()
- {
- return (T)Activator.CreateInstance(typeof(T));
- }
- }
- // The object we are going to Serializing/De-serializing
- [XmlInclude(typeof(Line))]
- [XmlInclude(typeof(Circle))]
- [XmlInclude(typeof(Square))]
- [Serializable()]
- public class DrawingObject
- {
- public virtual string Draw()
- {
- return "generic drawing object.";
- }
- }
- [Serializable()]
- public class Square : DrawingObject
- {
- public override string Draw()
- {
- return "Square.";
- }
- public Square()
- {
- }
- public Square(int x1, int y1, int x2, int y2)
- {
- X1 = x1;
- Y1 = y1;
- X2 = x2;
- Y2 = y2;
- }
- public int X1 { get; set; }
- public int Y1 { get; set; }
- public int X2 { get; set; }
- public int Y2 { get; set; }
- }
- [Serializable()]
- public class Line : DrawingObject
- {
- public override string Draw()
- {
- return "Line.";
- }
- public Line()
- {
- }
- public Line(int x, int y)
- {
- this.X = x;
- this.Y = y;
- }
- public int X { get; set; }
- public int Y { get; set; }
- }
- [Serializable()]
- public class Circle : DrawingObject
- {
- public override string Draw()
- {
- return "Circle.";
- }
- public Circle()
- {
- }
- public Circle(int radius, int x, int y)
- {
- this.Radius = radius;
- this.X = x;
- this.Y = y;
- }
- public int Radius { get; set; }
- public int X { get; set; }
- public int Y { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement