declaring xmlserializer where typeof is a class derived from arraylist
-
hi, if i have a class like below:
[XmlRoot("MyData")] public class MyData:ArrayList { 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; }.......
In the Form:MyData data=new MyData(); XmlSerializer s=new XmlSerializer(typeof(MyData));
generates the error:An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting type 'DrawShape.MyData'.
How can i declare XmlSerializer for a Class MyData which inherits ArrayList class? Thanks in advance -
hi, if i have a class like below:
[XmlRoot("MyData")] public class MyData:ArrayList { 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; }.......
In the Form:MyData data=new MyData(); XmlSerializer s=new XmlSerializer(typeof(MyData));
generates the error:An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting type 'DrawShape.MyData'.
How can i declare XmlSerializer for a Class MyData which inherits ArrayList class? Thanks in advanceHi cyn8 At first I couldn't understand why you derived MyData from ArrayList !! and the problem : it is one of the boring and annoying one.I tested and my brain was going to blown (thank god for saving me) it seems it happens because xmlSerializer can just serialize public class,and it's public memebers no more no less here is a few things must be paid attention for xml serializtion 1-your class must be public 2-your class must have parameterless constructor 3-all members are chosen must be public and if they are property type they must have both get and set 4-if your class would be embedded in another class Xml in serialization the embedded class must have XmlInclude Attribute or in the outer class type of inner class must be passed through (another way is pass it's type throught XmlSerializer constructor in extraTypes argument but I'm not sure it works well) hope the post would be useful good luck :)
-
hi, if i have a class like below:
[XmlRoot("MyData")] public class MyData:ArrayList { 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; }.......
In the Form:MyData data=new MyData(); XmlSerializer s=new XmlSerializer(typeof(MyData));
generates the error:An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There was an error reflecting type 'DrawShape.MyData'.
How can i declare XmlSerializer for a Class MyData which inherits ArrayList class? Thanks in advanceand these are the codes I modified and tested (I'm really sorry about what I sent two days ago it didn't work well in desrialization :( ) Here is Correct ones
\[XmlRoot("MyData")\] \[Serializable\] public class MyData { //it seems datas in each arraylist are the same type so if you're codinh in .NET2.0 or above use List<> instead of ArrayList ArrayList shapeTypeList; ArrayList colorList; ArrayList coordList; ArrayList sizeList; ArrayList graphicsPathDataList; \[XmlArray("ShapeData")\] \[XmlArrayItem("Shape")\] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } set { this.shapeTypeList = value; } } \[XmlArray("ColorData")\] \[XmlArrayItem("Color")\] public ArrayList ColorList { get { return this.colorList; } set { this.colorList = value; } } \[XmlArray("CoorinationData")\] \[XmlArrayItem("Coordination")\] public ArrayList CoordList { get { return this.coordList; } set { this.coordList= value; } } \[XmlArray("SizeData")\] \[XmlArrayItem("Size")\] public ArrayList SizeList { get { return this.sizeList; } set { this.sizeList = value; } } \[XmlArray("GPDDataCollection")\] \[XmlArrayItem("GPD",Type=typeof(GraphicsPathData))\] public ArrayList GraphicsPathDataList { get { return this.graphicsPathDataList; } set { this.graphicsPathDataList = value; } } 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.graphicsPathDataList = gpd; } private MyData() { } static MyData BinLoad(string path) { BinaryFormatter binSer=new BinaryFormatter(); FileStream fo=new FileStream(path,FileMode.Open); object data=binSer.Deserialize(fo)
-
and these are the codes I modified and tested (I'm really sorry about what I sent two days ago it didn't work well in desrialization :( ) Here is Correct ones
\[XmlRoot("MyData")\] \[Serializable\] public class MyData { //it seems datas in each arraylist are the same type so if you're codinh in .NET2.0 or above use List<> instead of ArrayList ArrayList shapeTypeList; ArrayList colorList; ArrayList coordList; ArrayList sizeList; ArrayList graphicsPathDataList; \[XmlArray("ShapeData")\] \[XmlArrayItem("Shape")\] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } set { this.shapeTypeList = value; } } \[XmlArray("ColorData")\] \[XmlArrayItem("Color")\] public ArrayList ColorList { get { return this.colorList; } set { this.colorList = value; } } \[XmlArray("CoorinationData")\] \[XmlArrayItem("Coordination")\] public ArrayList CoordList { get { return this.coordList; } set { this.coordList= value; } } \[XmlArray("SizeData")\] \[XmlArrayItem("Size")\] public ArrayList SizeList { get { return this.sizeList; } set { this.sizeList = value; } } \[XmlArray("GPDDataCollection")\] \[XmlArrayItem("GPD",Type=typeof(GraphicsPathData))\] public ArrayList GraphicsPathDataList { get { return this.graphicsPathDataList; } set { this.graphicsPathDataList = value; } } 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.graphicsPathDataList = gpd; } private MyData() { } static MyData BinLoad(string path) { BinaryFormatter binSer=new BinaryFormatter(); FileStream fo=new FileStream(path,FileMode.Open); object data=binSer.Deserialize(fo)
hi, i'm sorry to say that i'm still stuck. The XML file can be generated but it is the same as shown before where there is no root and all the element is "ArrayOfAnyType"
[XmlRoot("MyData")] public class MyData { ArrayList shapeTypeList; ArrayList colorList; ArrayList coordList; ArrayList sizeList; ArrayList arrS; [XmlArray("ShapeData")] [XmlArrayItem("Shape",typeof(ShapeL))] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } set{this.shapeTypeList=value;} } public class ShapeL { public ShapeL(){} public TypeOfShape c { get{return c;} set{c=value;} } } [XmlArray("ColorData")] [XmlArrayItem("Color",typeof(ColorL))] public ArrayList ColorList { get { return this.colorList; } set{this.colorList=value;} } public class ColorL { public ColorL(){} [XmlElement("color")] public Color c { get{return c;} set{c=value;} } } [XmlArray("CoordinateData")] [XmlArrayItem("Coordinate",typeof(Coordinate))] public ArrayList CoordList { get { return this.coordList; } set{this.coordList=value;} } public class Coordinate { public Coordinate(){} public Point[] point { get{return point;} set{point=value;} } } [XmlArray("SizeData")] [XmlArrayItem("Size",typeof(SizeL))] public ArrayList SizeList { get { return this.sizeList; } set{this.sizeList=value;} } public class SizeL { public SizeL(){} public Size c { get{return c;} set{c=value;} } } [XmlArray("GPDData")] [XmlArrayItem("GPD",typeof(ArrSave))] public ArrayList ArrS { get { return this.arrS; } set{ this.arrS=value;} } public class ArrSave { public ArrSave(){} public MemoryStream c { get{return c;} set{c=value;} } } 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), type
-
hi, i'm sorry to say that i'm still stuck. The XML file can be generated but it is the same as shown before where there is no root and all the element is "ArrayOfAnyType"
[XmlRoot("MyData")] public class MyData { ArrayList shapeTypeList; ArrayList colorList; ArrayList coordList; ArrayList sizeList; ArrayList arrS; [XmlArray("ShapeData")] [XmlArrayItem("Shape",typeof(ShapeL))] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } set{this.shapeTypeList=value;} } public class ShapeL { public ShapeL(){} public TypeOfShape c { get{return c;} set{c=value;} } } [XmlArray("ColorData")] [XmlArrayItem("Color",typeof(ColorL))] public ArrayList ColorList { get { return this.colorList; } set{this.colorList=value;} } public class ColorL { public ColorL(){} [XmlElement("color")] public Color c { get{return c;} set{c=value;} } } [XmlArray("CoordinateData")] [XmlArrayItem("Coordinate",typeof(Coordinate))] public ArrayList CoordList { get { return this.coordList; } set{this.coordList=value;} } public class Coordinate { public Coordinate(){} public Point[] point { get{return point;} set{point=value;} } } [XmlArray("SizeData")] [XmlArrayItem("Size",typeof(SizeL))] public ArrayList SizeList { get { return this.sizeList; } set{this.sizeList=value;} } public class SizeL { public SizeL(){} public Size c { get{return c;} set{c=value;} } } [XmlArray("GPDData")] [XmlArrayItem("GPD",typeof(ArrSave))] public ArrayList ArrS { get { return this.arrS; } set{ this.arrS=value;} } public class ArrSave { public ArrSave(){} public MemoryStream c { get{return c;} set{c=value;} } } 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), type
I Saw your code you didn't pay attention to rules I said before IF YOU SERIALIZE AN ARRAYLIST DIRECTLY IT WOULD GIVE YOU THE RESULTS LIKE THAT! MyData class actually is scheme for xmlSerializer so you don't need to put each array in serializer (in addition I said before retriving the data would be difficult too) just put MyData Class in Serializer and all memebers you marked with attributes would be serialized another thing is Color Struct properties are readonly so you can't seriailize it with xmlSerializer you must create a new Color Class with specific attributes and Please don't serialize GraphicpathData and then serialize the streams again with xmlserializer i have doubt it works in Deserializing atall and ofcourse it waste the Resources at last i put a complete code in http://hjk.4shared.com or direct link http://www.4shared.com/file/22596905/834df73f/XmlSerializationExample.html[^] good luck
-
I Saw your code you didn't pay attention to rules I said before IF YOU SERIALIZE AN ARRAYLIST DIRECTLY IT WOULD GIVE YOU THE RESULTS LIKE THAT! MyData class actually is scheme for xmlSerializer so you don't need to put each array in serializer (in addition I said before retriving the data would be difficult too) just put MyData Class in Serializer and all memebers you marked with attributes would be serialized another thing is Color Struct properties are readonly so you can't seriailize it with xmlSerializer you must create a new Color Class with specific attributes and Please don't serialize GraphicpathData and then serialize the streams again with xmlserializer i have doubt it works in Deserializing atall and ofcourse it waste the Resources at last i put a complete code in http://hjk.4shared.com or direct link http://www.4shared.com/file/22596905/834df73f/XmlSerializationExample.html[^] good luck
-
Are you sure?? :confused: I test them and they works here they are again http://hjk.4shared.com and there is xmlSerializationExample.Zip there after clicking on it to download in download page yuo must wait alittle and the DownloadFile link would appear here is the direct link to file http://www.4shared.com/file/22596905/834df73f/XmlSerializationExample.html here is the new link http://rapidshare.com/files/50526458/XmlSerializationExample.zip.html AS A TIP YOU CANNOT DOWNLOAD FROM THESE LINKS WITH DOWNLOAD MANAGERS AND ACCELERATORS and sorry if you had problem in downloading them just give your mail if you want hope these links work :)
-
Are you sure?? :confused: I test them and they works here they are again http://hjk.4shared.com and there is xmlSerializationExample.Zip there after clicking on it to download in download page yuo must wait alittle and the DownloadFile link would appear here is the direct link to file http://www.4shared.com/file/22596905/834df73f/XmlSerializationExample.html here is the new link http://rapidshare.com/files/50526458/XmlSerializationExample.zip.html AS A TIP YOU CANNOT DOWNLOAD FROM THESE LINKS WITH DOWNLOAD MANAGERS AND ACCELERATORS and sorry if you had problem in downloading them just give your mail if you want hope these links work :)
THANK YOU!!! You are a LIFE SAVER!!! Man, it is the people like you that makes this world a better place to live in:P Your codes works like a charm. However, there is a small problem, there seem to extra shape(exactly same shape) that is overlap on each shape. When i drag and drop on of the shape, the extra shape is temporary will be display at the original coordinate. But later if i want to drag the extra shape, it cannot be dragged and sometimes after i dragged a shape, all the extra shapes that is position on the saved shape original position will disappear. What you think that it is caused by? Below are the codes in the Load function located in the form( ShapeTypeList,CoordList,SizeList are arraylist that contains the data for the shapes to be drawn on the form):
OpenFileDialog openFileDialog = new OpenFileDialog(); ArrayList arr =new ArrayList(); ArrayList ColorArr =new ArrayList(); if (openFileDialog.ShowDialog() == DialogResult.OK) { filename = openFileDialog.FileName; ClearArrayList(); MyData deserializeData =MyData.Load(filename); ShapeTypeList=deserializeData.ShapeTypeList; CoordList=deserializeData.CoordList; ColorArr=deserializeData.ColorList; SizeList=deserializeData.SizeList; arr=deserializeData.GraphicsPathData; for(int j=0;j What should i do to solve this problem? Thanks a million:) -- modified at 6:53 Thursday 23rd August, 2007
-
THANK YOU!!! You are a LIFE SAVER!!! Man, it is the people like you that makes this world a better place to live in:P Your codes works like a charm. However, there is a small problem, there seem to extra shape(exactly same shape) that is overlap on each shape. When i drag and drop on of the shape, the extra shape is temporary will be display at the original coordinate. But later if i want to drag the extra shape, it cannot be dragged and sometimes after i dragged a shape, all the extra shapes that is position on the saved shape original position will disappear. What you think that it is caused by? Below are the codes in the Load function located in the form( ShapeTypeList,CoordList,SizeList are arraylist that contains the data for the shapes to be drawn on the form):
OpenFileDialog openFileDialog = new OpenFileDialog(); ArrayList arr =new ArrayList(); ArrayList ColorArr =new ArrayList(); if (openFileDialog.ShowDialog() == DialogResult.OK) { filename = openFileDialog.FileName; ClearArrayList(); MyData deserializeData =MyData.Load(filename); ShapeTypeList=deserializeData.ShapeTypeList; CoordList=deserializeData.CoordList; ColorArr=deserializeData.ColorList; SizeList=deserializeData.SizeList; arr=deserializeData.GraphicsPathData; for(int j=0;j What should i do to solve this problem? Thanks a million:) -- modified at 6:53 Thursday 23rd August, 2007
Hi at first thank you :). and about the problem I don't know I get it or not but it seems you want to draw eeah sahpe in a single path right?
GraphicsPath myPath=new GraphicsPath();
int k=0;
for(int i=0;i {
if((TypeOfShape)ShapeTypeList[i]==TypeOfShape.Oval||(TypeOfShape)ShapeTypeList[i]==TypeOfShape.Circle)
{
Point[] p=(Point[])CoordList[i];
Size s=(Size)SizeList[k];
myPath.AddEllipse(p[0].X,p[0].Y,s.Width,s.Height);
k++;
}else
{
myPath.AddPolygon((Point[])CoordList[i]);
}PathList.Add(myPath);
}//this.Refresh();
panel1.Invalidate();
}in the code above I marked some lines as red if i was right about what you want to do in the first red line you create an instance of the GraphicPath and darw all of the shapes inside it but in the second red line with ireratinf in the shapes you add it to a arrayList so infact you draw all your shapes in just ine Graphic path and then for each shape add it to the arraylist so you have same Graphicpath as many as your shapes and you draw it I think on your test so it wll make extra shape! for solving this simply put the first red line in the loop and it would create ab instance of Graphicpath each time and prevent from extra shapes. hope this helps Best regards:)
-
Hi at first thank you :). and about the problem I don't know I get it or not but it seems you want to draw eeah sahpe in a single path right?
GraphicsPath myPath=new GraphicsPath();
int k=0;
for(int i=0;i {
if((TypeOfShape)ShapeTypeList[i]==TypeOfShape.Oval||(TypeOfShape)ShapeTypeList[i]==TypeOfShape.Circle)
{
Point[] p=(Point[])CoordList[i];
Size s=(Size)SizeList[k];
myPath.AddEllipse(p[0].X,p[0].Y,s.Width,s.Height);
k++;
}else
{
myPath.AddPolygon((Point[])CoordList[i]);
}PathList.Add(myPath);
}//this.Refresh();
panel1.Invalidate();
}in the code above I marked some lines as red if i was right about what you want to do in the first red line you create an instance of the GraphicPath and darw all of the shapes inside it but in the second red line with ireratinf in the shapes you add it to a arrayList so infact you draw all your shapes in just ine Graphic path and then for each shape add it to the arraylist so you have same Graphicpath as many as your shapes and you draw it I think on your test so it wll make extra shape! for solving this simply put the first red line in the loop and it would create ab instance of Graphicpath each time and prevent from extra shapes. hope this helps Best regards:)
-
Hi, Thanks...Your solution is just what i need to do! Now it seems that all is working properly:) Thanks and best regards:rose: Have a nice day:-D
your welcome :) have a nice day too