Serialization problem
-
I tried simple serialization as this:
/// /// Keeps objects of type DataGridRow /// ArrayList RouteData = new ArrayList(); ... publicvoid Serialize() { DataGridRow[] dgr = newDataGridRow[RouteData.Count]; for (int i = 0; i < RouteData.Count; i++) { dgr[i] = (DataGridRow)RouteData[i]; } XmlSerializer a = newXmlSerializer(dgr.GetType()); StreamWriter sw = newStreamWriter(@"c:/aaa.txt"); a.Serialize(sw, RouteData); }
I`m getting error... What do you think may be wrong? thank you in advance for any help -
I tried simple serialization as this:
/// /// Keeps objects of type DataGridRow /// ArrayList RouteData = new ArrayList(); ... publicvoid Serialize() { DataGridRow[] dgr = newDataGridRow[RouteData.Count]; for (int i = 0; i < RouteData.Count; i++) { dgr[i] = (DataGridRow)RouteData[i]; } XmlSerializer a = newXmlSerializer(dgr.GetType()); StreamWriter sw = newStreamWriter(@"c:/aaa.txt"); a.Serialize(sw, RouteData); }
I`m getting error... What do you think may be wrong? thank you in advance for any help-Yoyosh- wrote:
I`m getting error...
What error do you get?
-Yoyosh- wrote:
What do you think may be wrong?
Maybe they are just typos or somethings gone wrong while copying code but you should definitely put spaces behind every
new
keyword.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
-Yoyosh- wrote:
I`m getting error...
What error do you get?
-Yoyosh- wrote:
What do you think may be wrong?
Maybe they are just typos or somethings gone wrong while copying code but you should definitely put spaces behind every
new
keyword.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
1."There was an error generating the XML document." InvalidOperationException 2.This is just copy/paste issue, all code is being compiled successfully, error occurs at run-time
-
Did u declare the class or the method as [Serializable]?
Everyone is a genius at least once a year - Pramod
You right, I forgot. I mark both classes with that attribute - the class, that elemets` are kept in this structure, and class that contains Serialize method. I still get exactly the same error Please note that I cannot mark a method with that attribute: "Error 2 Attribute 'Serializable' is not valid on this declaration type. It is valid on 'class, struct, enum, delegate' declarations only. " Do you have any idea, or maybe i did something wrong?
-
You right, I forgot. I mark both classes with that attribute - the class, that elemets` are kept in this structure, and class that contains Serialize method. I still get exactly the same error Please note that I cannot mark a method with that attribute: "Error 2 Attribute 'Serializable' is not valid on this declaration type. It is valid on 'class, struct, enum, delegate' declarations only. " Do you have any idea, or maybe i did something wrong?
-
You don't need to declare the method as Serializable, Declare only the classes as Serializable. If u still get error please paste the code here so that we can see wht's going wrong. Regards,
Everyone is a genius at least once a year - Pramod
Sure, this is class, which object`s are kept in array/arraylist:
[Serializable] public class DataGridRow { public int Time; public float Speed; public DataGridRow(int t, float s) { Time = t; Speed = s; } public DataGridRow() { } }
And this is part of other class, that performs serialization:/// /// Keeps objects of type DataGridRow /// ArrayList RouteData = new ArrayList(); (...) public void Serialize() { DataGridRow[] dgr = new DataGridRow[RouteData.Count]; for (int i = 0; i < RouteData.Count; i++) { dgr[i] = (DataGridRow)RouteData[i]; } XmlSerializer a = new XmlSerializer(dgr.GetType()); StreamWriter sw = new StreamWriter(@"c:/aaa.txt"); a.Serialize(sw, RouteData); }
Please let me know if I missed something (I may send it to you if you like) -
Sure, this is class, which object`s are kept in array/arraylist:
[Serializable] public class DataGridRow { public int Time; public float Speed; public DataGridRow(int t, float s) { Time = t; Speed = s; } public DataGridRow() { } }
And this is part of other class, that performs serialization:/// /// Keeps objects of type DataGridRow /// ArrayList RouteData = new ArrayList(); (...) public void Serialize() { DataGridRow[] dgr = new DataGridRow[RouteData.Count]; for (int i = 0; i < RouteData.Count; i++) { dgr[i] = (DataGridRow)RouteData[i]; } XmlSerializer a = new XmlSerializer(dgr.GetType()); StreamWriter sw = new StreamWriter(@"c:/aaa.txt"); a.Serialize(sw, RouteData); }
Please let me know if I missed something (I may send it to you if you like)OK, After going through the above code what i think is that when u are serializing u have to fetch each item from the Arraylist and serialize each object which is of the class DataGridRow, and also mention the class name in the XML Serializer constructor. Also change the file type i.e. c:\aaa.xml Try the above changes, if it works well and good if not just repost the error that u get and send the project so that we can debug and check what's going wrong.
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
OK, After going through the above code what i think is that when u are serializing u have to fetch each item from the Arraylist and serialize each object which is of the class DataGridRow, and also mention the class name in the XML Serializer constructor. Also change the file type i.e. c:\aaa.xml Try the above changes, if it works well and good if not just repost the error that u get and send the project so that we can debug and check what's going wrong.
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
At begin I wanted to serialize the ArrayList with all elements included. Then some guy told me, that ArrayList objects can`t be serialized, but I`m sure that 1D arrays are. So thats why I copied all elements from ArrayList to Array of type DataGridRow. So I don`t think there is a must to serialize each object separately... I changed file name to aaa.xml, still I get the same error. Here you can download this whole project (112kb) thank you very much for help
-
At begin I wanted to serialize the ArrayList with all elements included. Then some guy told me, that ArrayList objects can`t be serialized, but I`m sure that 1D arrays are. So thats why I copied all elements from ArrayList to Array of type DataGridRow. So I don`t think there is a must to serialize each object separately... I changed file name to aaa.xml, still I get the same error. Here you can download this whole project (112kb) thank you very much for help
ok what i had done is i created a class declared that serializable, declared the properties and wrote a method which was used to set values to the properties and also wrote the serialize method in the same class and then in the serialize method i used this object to serialize. The link that u have sent is blank and i could not find any project there.
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
ok what i had done is i created a class declared that serializable, declared the properties and wrote a method which was used to set values to the properties and also wrote the serialize method in the same class and then in the serialize method i used this object to serialize. The link that u have sent is blank and i could not find any project there.
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
-
At begin I wanted to serialize the ArrayList with all elements included. Then some guy told me, that ArrayList objects can`t be serialized, but I`m sure that 1D arrays are. So thats why I copied all elements from ArrayList to Array of type DataGridRow. So I don`t think there is a must to serialize each object separately... I changed file name to aaa.xml, still I get the same error. Here you can download this whole project (112kb) thank you very much for help
-
one important thing that u have forgot is you have to declare the class as [XMLRoot "classname"] and the properties as [XMLElement "name"] try this
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
I added these attributes - still the same result :( The link was correct, I tested it, but maybe server was down at the time you tried it. Nevertheless - this is the same project on another server: Download (I tested it and it worked) Or, if you still develope some problems with downloading, please supply mi with your @mail address again, thank you very much for helping
-
I added these attributes - still the same result :( The link was correct, I tested it, but maybe server was down at the time you tried it. Nevertheless - this is the same project on another server: Download (I tested it and it worked) Or, if you still develope some problems with downloading, please supply mi with your @mail address again, thank you very much for helping
-
I added these attributes - still the same result :( The link was correct, I tested it, but maybe server was down at the time you tried it. Nevertheless - this is the same project on another server: Download (I tested it and it worked) Or, if you still develope some problems with downloading, please supply mi with your @mail address again, thank you very much for helping
-
i have made the neccessary changes and it's working fine can u please send me your email address, so that i can send u the project.
Thanks & Regards, Pramod "Everyone is a genius at least once a year"
it is: yoyosh@o2.pl