problem in xmlserialization for arraylist
-
hi, After i serialize a few arraylist, my xml file seem to look like below:
1 40 18 110 18 110 68 40 68 142 256
is this an error? why does is the elements is named '' Besides, how do i deserialize arraylist of different types. first i declare:XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} );
then in Load function:filename = openFileDialog.FileName; Stream myStream = openFileDialog.OpenFile(); ShapeTypeList = (ArrayList) s.Deserialize(myStream); ColorList = (ArrayList) s.Deserialize(myStream); CoordList = (ArrayList)s.Deserialize(myStream); SizeList = (ArrayList) s.Deserialize(myStream); arr=(ArrayList) s.Deserialize(myStream);
thanks for any reply:) -
hi, After i serialize a few arraylist, my xml file seem to look like below:
1 40 18 110 18 110 68 40 68 142 256
is this an error? why does is the elements is named '' Besides, how do i deserialize arraylist of different types. first i declare:XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} );
then in Load function:filename = openFileDialog.FileName; Stream myStream = openFileDialog.OpenFile(); ShapeTypeList = (ArrayList) s.Deserialize(myStream); ColorList = (ArrayList) s.Deserialize(myStream); CoordList = (ArrayList)s.Deserialize(myStream); SizeList = (ArrayList) s.Deserialize(myStream); arr=(ArrayList) s.Deserialize(myStream);
thanks for any reply:)As far as i know if you put the attributes directly on the arraylist they don't work for giving the alternate name you set you must put them on classes and properties here is an exapmle if you write your code somewhat like this [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] ArrayList arr = new ArrayList(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); XmlSerializer xmlSer = new XmlSerializer(arr.GetType()); FileStream fo=new FileStream(@"N:\xmlserTest.xml",FileMode.OpenOrCreate); xmlSer.Serialize(fo,arr); fo.Close(); } the result would be like Manoj 4 4.5 and if you write it like this [XmlRoot("MyData")] public class MyData { ArrayList arr = new ArrayList(); [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] public ArrayList TheData { get { return this.arr; } } public MyData() { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); } public void Serialize() { XmlSerializer xmlSer = new XmlSerializer(typeof(MyData)); FileStream fo = new FileStream(@"N:\xmlserTest.xml", FileMode.OpenOrCreate); xmlSer.Serialize(fo, this); fo.Close(); } } the result would be Manoj 4 4.5 as a tip the XmlRoot Attribute just replace the name of the class with the alternate name hope the post would be useful good luck
-
As far as i know if you put the attributes directly on the arraylist they don't work for giving the alternate name you set you must put them on classes and properties here is an exapmle if you write your code somewhat like this [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] ArrayList arr = new ArrayList(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); XmlSerializer xmlSer = new XmlSerializer(arr.GetType()); FileStream fo=new FileStream(@"N:\xmlserTest.xml",FileMode.OpenOrCreate); xmlSer.Serialize(fo,arr); fo.Close(); } the result would be like Manoj 4 4.5 and if you write it like this [XmlRoot("MyData")] public class MyData { ArrayList arr = new ArrayList(); [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] public ArrayList TheData { get { return this.arr; } } public MyData() { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); } public void Serialize() { XmlSerializer xmlSer = new XmlSerializer(typeof(MyData)); FileStream fo = new FileStream(@"N:\xmlserTest.xml", FileMode.OpenOrCreate); xmlSer.Serialize(fo, this); fo.Close(); } } the result would be Manoj 4 4.5 as a tip the XmlRoot Attribute just replace the name of the class with the alternate name hope the post would be useful good luck
Hi, below is how i declare my XmlRoot. However the error is still the same as before. Should i create another class such as MyData class as you shown before? [XmlRoot("Form")] public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Panel panel1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; ColorDialog clg = new ColorDialog(); XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} ); ArrayList ShapeTypeList=new ArrayList (); ArrayList ColorList=new ArrayList (); private ArrayList alDrawingObjects = new ArrayList(); ArrayList CoordList=new ArrayList(); //[XmlArray("CoordinateData")] [XmlElement("Coordinate",typeof(Coordinate))] public ArrayList coordList { get{return CoordList;} set{CoordList=value;} } public class Coordinate { public Coordinate(){} public Point[] point { get{return point;} set{point=value;} } }............ Thanks for the reply:)
-
As far as i know if you put the attributes directly on the arraylist they don't work for giving the alternate name you set you must put them on classes and properties here is an exapmle if you write your code somewhat like this [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] ArrayList arr = new ArrayList(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); XmlSerializer xmlSer = new XmlSerializer(arr.GetType()); FileStream fo=new FileStream(@"N:\xmlserTest.xml",FileMode.OpenOrCreate); xmlSer.Serialize(fo,arr); fo.Close(); } the result would be like Manoj 4 4.5 and if you write it like this [XmlRoot("MyData")] public class MyData { ArrayList arr = new ArrayList(); [XmlArray("DataArray")] [XmlArrayItem("DataArrayElement")] public ArrayList TheData { get { return this.arr; } } public MyData() { arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); } public void Serialize() { XmlSerializer xmlSer = new XmlSerializer(typeof(MyData)); FileStream fo = new FileStream(@"N:\xmlserTest.xml", FileMode.OpenOrCreate); xmlSer.Serialize(fo, this); fo.Close(); } } the result would be Manoj 4 4.5 as a tip the XmlRoot Attribute just replace the name of the class with the alternate name hope the post would be useful good luck
hi, now i've tried to create the MyData class. However, i'm stuck on how to change the codes in the form to do the serialization. below is some of the codes: MyData Class:
[XmlRoot("MyData")] public class MyData { public MyData() { } public MyData(ArrayList shape, ArrayList color, ArrayList coord, ArrayList size, ArrayList gpd) { this.ShapeTypeList = shape; this.ColorList = color; this.CoordList = coord; this.SizeList = size; this.arrS = gpd; } public enum TypeOfShape { Square, Rect, Parallelogram, Trapezoid, Diamond, Triangle, RightAngleTriangle,Circle,Oval,Hexagon,Pentagon,None } // XmlSerializer serializ() // { // XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} ); // return s; // } XmlSerializer s = new XmlSerializer( typeof(ArrayList),new Type[] {typeof(Coordinate),typeof(ColorL),typeof(SizeL), typeof(ArrSave)} ); ArrayList ShapeTypeList=new ArrayList (); ArrayList ColorList=new ArrayList (); ArrayList CoordList=new ArrayList(); ArrayList SizeList = new ArrayList(); ArrayList arrS = new ArrayList(); ArrayList PathList =new ArrayList(); //[XmlArray("CoordinateData")] [XmlElement("Coordinate",typeof(Coordinate))] public ArrayList coordList { get{return CoordList;} set{CoordList=value;} } public class Coordinate { public Coordinate(){} public Point[] point { get{return point;} set{point=value;} } }.....................
MovePosition Function in Form:private void MovePosition(int position,int BshapeNo,int sztotal,int szOrd) { if((TypeOfShape)ShapeTypeList[shapeNo]!=TypeOfShape.Circle&&(TypeOfShape)ShapeTypeList[shapeNo]!=TypeOfShape.Oval) { alDrawingObjects.Insert(position, new Polygon((Color)ColorList[shapeNo], (Point[])CoordList[shapeNo], 5,(GraphicsPath)PathList[shapeNo] )); } else if ((TypeOfShape)ShapeTypeList[shapeNo]==TypeOfShape.Circle||(TypeOfShape)ShapeTypeList[shapeNo]==TypeOfShape.Oval) { alDrawingObjects.Insert(position, new Ellipse((Color)ColorList[shapeNo],(Point[]) CoordList[shapeNo], (Size)SizeList[sizeOrder], 5) ); SizeList.Insert(sztotal,(Size)SizeList[sizeOrder]); SizeList.RemoveAt(szOrd); } CoordList.Insert(position, (Point[])CoordList[shapeNo]); ColorList.Insert(position,(Color)ColorList[shapeNo]); ShapeTypeList.Insert(position,(TypeOf