xml serialization for multiple arraylists
-
hi, i'm new to xml serialization. I was wondering how to serialize and deserialize a few('more than 1')arraylists. Another thing is that one of the arraylist consist of a list graphicspaths. I manage to accomplish this same task using binary serialization. However, now i want to learn on how to do this using xml serialization. Can anybody help? I can't seem to find much information about it. Thanks in advance.
-
hi, i'm new to xml serialization. I was wondering how to serialize and deserialize a few('more than 1')arraylists. Another thing is that one of the arraylist consist of a list graphicspaths. I manage to accomplish this same task using binary serialization. However, now i want to learn on how to do this using xml serialization. Can anybody help? I can't seem to find much information about it. Thanks in advance.
Do you want to serialize array of arraylists? As far as I remember xml serialization doesn't support serializing graohicspaths (But I'm not sure) If arraylist doesn't contain some complex type that can't be serialized by xml serialization then you can serialize it very easily. Here are some links to get you started: xml serialization[^] Serialize Arrays and ArrayLists to XML[^]
#region signature my articles #endregion
-
hi, i'm new to xml serialization. I was wondering how to serialize and deserialize a few('more than 1')arraylists. Another thing is that one of the arraylist consist of a list graphicspaths. I manage to accomplish this same task using binary serialization. However, now i want to learn on how to do this using xml serialization. Can anybody help? I can't seem to find much information about it. Thanks in advance.
Hi Cyn8 I said how you can serialize graphicPaths (if you had problem with it just tell me) before and about the arraylists you can just create a new Class that contains those data (is much better) like collection or just simply put your arrayLists niside another one and serialize that (I think I said these before too) but if you mean SOAP serializtion which give you the xml file just simply use SOAPFormatter Class instead of BinaryFormatter class and you don't need to change your code but as I know Xmls from SOAP are not such a readable ones So if you need to Save your data which be easily readable you can use xml.serializations which Giorgi Dalakishvili sent you some cool links about that or just create your custom data saver with xml classes if your datas are not such complex and at last for using SOAPFormatter you must add it as reference best regards -- modified at 10:07 Friday 17th August, 2007
-
Hi Cyn8 I said how you can serialize graphicPaths (if you had problem with it just tell me) before and about the arraylists you can just create a new Class that contains those data (is much better) like collection or just simply put your arrayLists niside another one and serialize that (I think I said these before too) but if you mean SOAP serializtion which give you the xml file just simply use SOAPFormatter Class instead of BinaryFormatter class and you don't need to change your code but as I know Xmls from SOAP are not such a readable ones So if you need to Save your data which be easily readable you can use xml.serializations which Giorgi Dalakishvili sent you some cool links about that or just create your custom data saver with xml classes if your datas are not such complex and at last for using SOAPFormatter you must add it as reference best regards -- modified at 10:07 Friday 17th August, 2007
hi there, With your help last time, i've manage to use binary serialization to serialize and deserialize the arraylists:) However, now i have abit problem with xml.serializations. Looking at the examples given, it seems that for each arraylist i need to create a class each? If it is so, which class should is labelled as "XmlRoot"? in an XML file there should have only one root and can have many elements right? Sorry if i still don't quite understand. Besides, i try to modify the codes by replacing all the functions that uses binary formatter to xml serializer, which does not works as shown below.
XmlSerializer s = new XmlSerializer( typeof(ArrayList) ); private ArrayList alDrawingObjects = new ArrayList(); public ArrayList CoordList=new ArrayList (); public ArrayList ShapeTypeList=new ArrayList (); public ArrayList ColorList=new ArrayList (); public ArrayList PolyCoord = new ArrayList(); public ArrayList SizeList = new ArrayList();
Serializing:Stream myStream ; myStream = File.OpenWrite(filename); ArrayList arr = new ArrayList(); if (myStream != null) { TextWriter w = new StreamWriter( filename ); s.Serialize( w, ShapeTypeList ); s.Serialize( w, ColorList ); s.Serialize( w, CoordList); s.Serialize( w, SizeList ); for(int i=0;i Deserializing: `ArrayList newList; TextReader r = new StreamReader(filename); 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); for(int i=0;i Please help to explain on which part i should change? Thanks again. -- modified at 2:42 Monday 20th August, 2007`
-
Do you want to serialize array of arraylists? As far as I remember xml serialization doesn't support serializing graohicspaths (But I'm not sure) If arraylist doesn't contain some complex type that can't be serialized by xml serialization then you can serialize it very easily. Here are some links to get you started: xml serialization[^] Serialize Arrays and ArrayLists to XML[^]
#region signature my articles #endregion
Actually i have a few arraylist which each contains different types or classes. Last time u use binary serialization, i can serialize each or the arraylist separately. e.g:
formatter.Serialize(myStream, arr1) formatter.Serialize(myStream, arr2) formatter.Serialize(myStream, arr3) //To restore the arraylists, make the calls to formatter.Deserialize in the //exact same order: arr1 = (ArrayList) formatter.Deserialize(myStream); arr2 = (ArrayList) formatter.Deserialize(myStream); arr3 = (ArrayList) formatter.Deserialize(myStream);
However, does this concept applies with xml serialization? Also, by looking at the examples given, it seems that for each arraylist i need to create a class each? If it is so, which class should is labelled as "XmlRoot"? if not mistaken, in an XML file there should have only one root. however, from my perspective, all of the arraylist should be xmlelements. Am i right? Thanks in advance. -
Hi Cyn8 I said how you can serialize graphicPaths (if you had problem with it just tell me) before and about the arraylists you can just create a new Class that contains those data (is much better) like collection or just simply put your arrayLists niside another one and serialize that (I think I said these before too) but if you mean SOAP serializtion which give you the xml file just simply use SOAPFormatter Class instead of BinaryFormatter class and you don't need to change your code but as I know Xmls from SOAP are not such a readable ones So if you need to Save your data which be easily readable you can use xml.serializations which Giorgi Dalakishvili sent you some cool links about that or just create your custom data saver with xml classes if your datas are not such complex and at last for using SOAPFormatter you must add it as reference best regards -- modified at 10:07 Friday 17th August, 2007
-
if i put the all arraylists into another arraylist, when i deserialize them, how do i deserialize them into the original arraylist? Thanks for any reply:)
I think the best way to do this is to create a new class for holding whole of your data. about the GPD you used in the code ,as a tip holding serialized streams is not a good choice ,I think you can serialize whole of your GPDs together (because they are marked as serializable) and in the code you put all your data in one stream ,retrieving from it is not impossible but is far more harder than use a new class to hold all of your data here is the scheme of such a class (it has some works to be a complete one)
[XmlRoot("MyData")] [Serializable] 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",typeof(/*you must get the type here*/))] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } } [XmlArray("ColorData")] [XmlArrayItem("Color",typeof(/*you must get the type here*/))] public ArrayList ColorList { get { return this.colorList; } } [XmlArray("CoorinationData")] [XmlArrayItem("Coordination",typeof(/*you must get the type here*/))] public ArrayList CoordList { get { return this.coordList; } } [XmlArray("SizeData")] [XmlArrayItem("Size",typeof(/*you must get the type here*/))] public ArrayList SizeList { get { return this.sizeList; } } [XmlArray("GPDData")] [XmlArrayItem("GPD",typeof(/*you must get the type here*/))] public ArrayList GraphicsPathDataList { get { return this.graphicsPathDataList; } } 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; } static MyData BinLoad(string path) {
-
I think the best way to do this is to create a new class for holding whole of your data. about the GPD you used in the code ,as a tip holding serialized streams is not a good choice ,I think you can serialize whole of your GPDs together (because they are marked as serializable) and in the code you put all your data in one stream ,retrieving from it is not impossible but is far more harder than use a new class to hold all of your data here is the scheme of such a class (it has some works to be a complete one)
[XmlRoot("MyData")] [Serializable] 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",typeof(/*you must get the type here*/))] public ArrayList ShapeTypeList { get { return this.shapeTypeList; } } [XmlArray("ColorData")] [XmlArrayItem("Color",typeof(/*you must get the type here*/))] public ArrayList ColorList { get { return this.colorList; } } [XmlArray("CoorinationData")] [XmlArrayItem("Coordination",typeof(/*you must get the type here*/))] public ArrayList CoordList { get { return this.coordList; } } [XmlArray("SizeData")] [XmlArrayItem("Size",typeof(/*you must get the type here*/))] public ArrayList SizeList { get { return this.sizeList; } } [XmlArray("GPDData")] [XmlArrayItem("GPD",typeof(/*you must get the type here*/))] public ArrayList GraphicsPathDataList { get { return this.graphicsPathDataList; } } 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; } static MyData BinLoad(string path) {
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 declareas shown below:(this is declare in Form, i did not use MyData class, instead all arraylist is declared in the form)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); arr=(ArrayList) s.Deserialize(myStream); for(int i=0;i