Click here to Skip to main content
12,064,291 members (46,847 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XML serialization
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...Smile | :)
god bless u all.. Smile | :)
Posted 1-Oct-12 0:51am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Chona1171 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

Advertise | Privacy | Mobile
Web01 | 2.8.160204.4 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100