Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem in xmlserialization for arraylist

problem in xmlserialization for arraylist

Scheduled Pinned Locked Moved C#
questionhelpxml
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyn8
    wrote on last edited by
    #1

    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:)

    H 1 Reply Last reply
    0
    • C cyn8

      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:)

      H Offline
      H Offline
      Hessam Jalali
      wrote on last edited by
      #2

      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

      C 2 Replies Last reply
      0
      • H Hessam Jalali

        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

        C Offline
        C Offline
        cyn8
        wrote on last edited by
        #3

        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:)

        1 Reply Last reply
        0
        • H Hessam Jalali

          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

          C Offline
          C Offline
          cyn8
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups