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. customize deserialization sample in C#?

customize deserialization sample in C#?

Scheduled Pinned Locked Moved C#
csharpxmlhelpquestion
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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I am looking for a sample, which could let me deserialize an XML stream encoded in UTF-8 into a field of a class. More specifically, I have a class like,

    class Foo
    {
        string abc;
        byte\[\] bcd;
    }
    

    and abc maps to XML element "Abc" and bcd maps to XML element "Bcd", and I want to get the stream for bcd and retrieve bytes (from XML stream for related element "Bcd" directly) to manipulate manually/in a customized way. I am looking for a sample, but failed, could anyone help to point me to a related sample or wrote some pseudo code? thanks in advance, George

    Z 1 Reply Last reply
    0
    • G George_George

      Hello everyone, I am looking for a sample, which could let me deserialize an XML stream encoded in UTF-8 into a field of a class. More specifically, I have a class like,

      class Foo
      {
          string abc;
          byte\[\] bcd;
      }
      

      and abc maps to XML element "Abc" and bcd maps to XML element "Bcd", and I want to get the stream for bcd and retrieve bytes (from XML stream for related element "Bcd" directly) to manipulate manually/in a customized way. I am looking for a sample, but failed, could anyone help to point me to a related sample or wrote some pseudo code? thanks in advance, George

      Z Offline
      Z Offline
      Zap Man
      wrote on last edited by
      #2

      using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap; [Serializable] class Customer { public string name; public long id; } class Test{ static void Main(string[] args) { ArrayList list = new ArrayList(); Customer cust = new Customer(); cust.name = "Charles Darwin"; cust.id = 10; list.Add(cust); cust = new Customer(); cust.name = "Isaac Newton"; cust.id = 20; list.Add(cust); foreach(Customer x in list){ Console.WriteLine("{0} : {1}", x.name.ToString(), x.id.ToString()); } Console.WriteLine("Saving Customer list"); FileStream s = new FileStream("cust.txt", FileMode.Create); SoapFormatter f = new SoapFormatter(); f.Serialize(s, list); s.Close(); Console.WriteLine("Restoring to New List"); s = new FileStream("cust.txt", FileMode.Open); ArrayList list2 = (ArrayList)f.Deserialize(s); s.Close(); foreach(Customer y in list2){ Console.WriteLine(y.name + ":" + y.id); } } } //You have to reference in using System.Runtime.Serialization.Formatters.Soap; //just right click on the reference folder and click add. Its under the web tab. //Let me know if this helps

      G 1 Reply Last reply
      0
      • Z Zap Man

        using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap; [Serializable] class Customer { public string name; public long id; } class Test{ static void Main(string[] args) { ArrayList list = new ArrayList(); Customer cust = new Customer(); cust.name = "Charles Darwin"; cust.id = 10; list.Add(cust); cust = new Customer(); cust.name = "Isaac Newton"; cust.id = 20; list.Add(cust); foreach(Customer x in list){ Console.WriteLine("{0} : {1}", x.name.ToString(), x.id.ToString()); } Console.WriteLine("Saving Customer list"); FileStream s = new FileStream("cust.txt", FileMode.Create); SoapFormatter f = new SoapFormatter(); f.Serialize(s, list); s.Close(); Console.WriteLine("Restoring to New List"); s = new FileStream("cust.txt", FileMode.Open); ArrayList list2 = (ArrayList)f.Deserialize(s); s.Close(); foreach(Customer y in list2){ Console.WriteLine(y.name + ":" + y.id); } } } //You have to reference in using System.Runtime.Serialization.Formatters.Soap; //just right click on the reference folder and click add. Its under the web tab. //Let me know if this helps

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks Zap-Man! I do not think your reply is directly related to my question. Here is a little more response. The XML is SOAP reaponse from server, in the response there is one response XML element (element Bcd in my sample) which is encoded by UTF-8 from server side, but since Http Web Services will use base64 at client side, so each time I receive such "bytes" at client side and the automatically generated web services proxy will throw exception says invalid XML element in base64 encoding. So, I am thinking about how to overwrite the default using base64 encoding to decode the bytes, and this is why I ask this question. If there could be a way to accept stream or something similar represents the on-wire bytes of the related response elements (Bcd in my sample) and let me manipulate, it will be so great! regards, George

        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