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. Serialization

Serialization

Scheduled Pinned Locked Moved C#
data-structuresxmljsonquestion
4 Posts 3 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.
  • U Offline
    U Offline
    User 16796
    wrote on last edited by
    #1

    Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.

    A N 2 Replies Last reply
    0
    • U User 16796

      Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.

      A Offline
      A Offline
      Andreas X
      wrote on last edited by
      #2

      thats it????? i'm glad for you......

      U 1 Reply Last reply
      0
      • A Andreas X

        thats it????? i'm glad for you......

        U Offline
        U Offline
        User 16796
        wrote on last edited by
        #3

        Yeah i got it i should Derive VariableCollection from CollectionBase and strongly Type the collection to be of Type Variable... Thanks TusharM

        1 Reply Last reply
        0
        • U User 16796

          Hi All, I have a class called Variable [Serializable] class Variable { public string varName; public string dataType; public string scope; public string comment; ...... } then i have a class VariableCollection [Serializable] public class VariableCollection { private ArrayList varCollection; //private Variable[] varCollection; public VariableCollection() { // // TODO: Add constructor logic here // varCollection = new ArrayList(); //varCollection = new LogicalBlock.Variable[100]; } public ArrayList VarCollection { get { return varCollection; } set { varCollection = value; } } } Now i want to serialize VariableCollection ,i will initialize ArrayList having Variable objects and give it to VariableCollection and then use following code to serialize VariableCollection LogicalBlock.VariableCollection varcollect = new LogicalBlock.VariableCollection(); varcollect = var;//var is of type Variable varcollect = var1;//var1 is of type Variable varcollect = var2;//var2 is of type Variable XmlSerializer serial = new XmlSerializer(varcollect.GetType(),"LogicalBlock"); Stream stream1 = new FileStream("MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None); serial.Serialize(stream1,varcollect); stream1.Close(); but it gives exception "System.InvalidOperationException" in VariableCollection if i use Array instead of ArrayList it works fine but i dont want to use Array. How should i Serialize(XML) collection having ArrayList and not Array?? Any Ideas?? Thanks in Advance. TusharM.

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          The ArrayList is Serializable, you should check out the XmlArrayAttribute[^] class, this can be used to tell your XmlSerializer to serialize a particular class member as an array of XML elements. - Nick Parker
          My Blog | My Articles

          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