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 problem

Serialization problem

Scheduled Pinned Locked Moved C#
helpwcflinuxxmljson
3 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.
  • N Offline
    N Offline
    NaNg15241
    wrote on last edited by
    #1

    Hello. So these are my problems: I have to find a way to save a class of mine to a file. I found serialization and I thought it might work. But... When I tried BinarySerialization, some chars would just disapear, and I'd get an exception for "not a correct format". XmlSerialization is throwing non-stopable exceptions, I just went to the one after. I tried SOAP serialization, but now I came across another problem. I need to serialize a generic collection which I built. This gives me an exception, saying that it cannot serialize a generic class. (that's after successfuly serializing List). Another thing that I have with SOAP serialization is that i'm not sure that it'll work in Linux (I was unable to install mono), since I need to import the SOAP class from Windows folder. Can anyone recommend me on what to do? Waiting for your help. NaNg. :)

    M 1 Reply Last reply
    0
    • N NaNg15241

      Hello. So these are my problems: I have to find a way to save a class of mine to a file. I found serialization and I thought it might work. But... When I tried BinarySerialization, some chars would just disapear, and I'd get an exception for "not a correct format". XmlSerialization is throwing non-stopable exceptions, I just went to the one after. I tried SOAP serialization, but now I came across another problem. I need to serialize a generic collection which I built. This gives me an exception, saying that it cannot serialize a generic class. (that's after successfuly serializing List). Another thing that I have with SOAP serialization is that i'm not sure that it'll work in Linux (I was unable to install mono), since I need to import the SOAP class from Windows folder. Can anyone recommend me on what to do? Waiting for your help. NaNg. :)

      M Offline
      M Offline
      mdv113
      wrote on last edited by
      #2

      I don’t really understand what the problem is you are experiencing but I could tell you how I serialize into a binary stream I stream can be send or saved to file. Serializing to XML goes the same way. Note both serializer and deserializer and the object definition of the object to serialize should be the same at the sending and receiving end. To ensure this I simply put the classes in a dll which both sides use. With dotnet 1.x the formatter will give an exception when a version difference is detected. In dotnet 2.0 the formatter will serialize everything it recognizes even if there are version differences. The class to serialize should be declared with [Serializable] /// /// Serialize object to byte array, object must be Serializable /// /// object to serialize /// array public byte[] Serialize(T someobject) { byte[] objectstream=null; BinaryFormatter f = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); f.Serialize(stream, someobject); stream.SetLength( stream.Position); objectstream = stream.GetBuffer(); return objectstream; } public void Deserialize(byte[] stream, out T someobject) { BinaryFormatter f = new BinaryFormatter(); MemoryStream serstream = new MemoryStream(stream); someobject = (T)f.Deserialize(serstream); } Class def of class to serialize: [Serializable] public class SetConfigMessage { public string Name = ""; public string Value = ""; /// /// Config message /// /// /// public SetConfigMessage() { } public SetConfigMessage(string name, string value) { this.Name = name; this.Value = value; } } madv113

      N 1 Reply Last reply
      0
      • M mdv113

        I don’t really understand what the problem is you are experiencing but I could tell you how I serialize into a binary stream I stream can be send or saved to file. Serializing to XML goes the same way. Note both serializer and deserializer and the object definition of the object to serialize should be the same at the sending and receiving end. To ensure this I simply put the classes in a dll which both sides use. With dotnet 1.x the formatter will give an exception when a version difference is detected. In dotnet 2.0 the formatter will serialize everything it recognizes even if there are version differences. The class to serialize should be declared with [Serializable] /// /// Serialize object to byte array, object must be Serializable /// /// object to serialize /// array public byte[] Serialize(T someobject) { byte[] objectstream=null; BinaryFormatter f = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); f.Serialize(stream, someobject); stream.SetLength( stream.Position); objectstream = stream.GetBuffer(); return objectstream; } public void Deserialize(byte[] stream, out T someobject) { BinaryFormatter f = new BinaryFormatter(); MemoryStream serstream = new MemoryStream(stream); someobject = (T)f.Deserialize(serstream); } Class def of class to serialize: [Serializable] public class SetConfigMessage { public string Name = ""; public string Value = ""; /// /// Config message /// /// /// public SetConfigMessage() { } public SetConfigMessage(string name, string value) { this.Name = name; this.Value = value; } } madv113

        N Offline
        N Offline
        NaNg15241
        wrote on last edited by
        #3

        My problem is that I can't serialize with binary formatter (even like you did, I can't), and SOAP doesn't let me serialize generic classes that I've made. I don't get what's hard to understand, I wrote it perfectly clear in the first message.

        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