Click here to Skip to main content
11,647,644 members (66,896 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 30-Sep-12 23:51pm
MAU7871.6K

1 solution

Rate this: bad
good
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; }
</pre>
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)
        {
            try
            {
                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;
            }
            catch
            {
                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;
        }
}

</t>
  Permalink  
Comments
MAU787 at 1-Oct-12 6:22am
   
thanx it actually solved my problem
Chona1171 at 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
0 jyo.net 500
1 F-ES Sitecore 370
2 OriginalGriff 322
3 DamithSL 315
4 CPallini 240
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web03 | 2.8.150804.3 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2015
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