Click here to Skip to main content
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 1-Oct-12 0:51am
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></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 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
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