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