Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all.
I am serializing my object / converting it to XML data
and passing to another page and again Deserializing it.

i am using following function to serialize and Deserializing
public static string Serialize(object objectToSerialize)
         MemoryStream mem = new MemoryStream();
         XmlSerializer ser = new XmlSerializer(objectToSerialize.GetType());
         ser.Serialize(mem, objectToSerialize);
         ASCIIEncoding ascii = new ASCIIEncoding();
         return ascii.GetString(mem.ToArray());

     public static object Deserialize(string xmlString)
         Type typeToDeserialize;
         byte[] bytes = Encoding.UTF8.GetBytes(xmlString);
         typeToDeserialize =xmlString.GetType();
         MemoryStream mem = new MemoryStream(bytes);
         XmlSerializer ser = new XmlSerializer(typeof (User ));
         return ser.Deserialize(mem);

i have object User as
public class User
       public string UserId { get; set; }
       public string Depatment { get; set; }
       public string Role { get; set; }
       internal byte[] FPTemplates { get; set; }

now when i am serializing it i m getting only first three elements
FPTemplates which is byte[] it is not present in xml file
what should i do to get full serialization of object.??

plz help me...
thank in advance...:)
god bless u all.. :)

1 solution

I see your issue.

Your byte array is internal make it public

<pre lang="c#">
public  byte[] FPTemplates { get; set; }

Try giving this a Go I see you are converting your string to utf8 and then ascii in your serialisers

/// <summary>
        /// Serialize an object into an XML string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject<t>(T obj)
                string xmlString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(T));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, obj);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                xmlString = Converters.ByteArrayToUTF8String(memoryStream.ToArray()); return xmlString;
                return string.Empty;

 /// <summary>
        /// Reconstruct an object from an XML string
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T DeserializeObject<t>(string xml)
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream memoryStream = new MemoryStream(Converters.StringToUTF8ByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);

public class Converters{
/// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String</param>
        /// <returns>String converted from Unicode Byte Array</returns>
        public static string ByteArrayToUTF8String(byte[] characters)
            UTF8Encoding encoding = new UTF8Encoding();
            string constructedString = encoding.GetString(characters);
            return (constructedString);
        /// <summary>
        /// Converts the String to UTF8 Byte array and is used in De serialization
        /// </summary>
        /// <param name="stringVal"></param>
        /// <returns></returns>
        public static Byte[] StringToUTF8ByteArray(string stringVal)
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(stringVal);
            return byteArray;

MAU787 1-Oct-12 6:22am
thanx it actually solved my problem
L Viljoen 1-Oct-12 6:23am
u welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900