Serializes and deserializes an entity to a base 64 string : Base64 « Development Class « C# / C Sharp






Serializes and deserializes an entity to a base 64 string

 

//
// Microsoft Developer & Platform Evangelism
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// Copyright (c) Microsoft Corporation.  All Rights Reserved.
// This code is released under the terms of the MS-LPL license, 
// http://microsoftnlayerapp.codeplex.com/license

using System;
using System.IO;
using System.Runtime.Serialization;

namespace Presentation.Web.MVC.Client.Extensions.Utilities
{
    /// <summary>
    /// This class serializes and deserializes an entity to a base 64 string.
    /// </summary>
    /// <typeparam name="T">Type of the entity to be serialized or deserialized.</typeparam>
    public class SelfTrackingEntityBase64Converter<T>
    {
        #region Members

        /// <summary>
        /// Deserializes the entity from a base 64 string.
        /// </summary>
        /// <param name="value">The base 64 string containing the serialized entity.</param>
        /// <returns>The resulting entity after deserialization.</returns>
        public T ToEntity(string value)
        {
            var bytes = Convert.FromBase64String(value);
            MemoryStream memStream = new MemoryStream(bytes);
            DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
            T result = (T)deserializer.ReadObject(memStream);
            return result;
        }

        /// <summary>
        /// Serialized the entity to a base 64 string.
        /// </summary>
        /// <param name="entity">The entity to be serialized.</param>
        /// <returns>The base 64 string containing the serialized entity.</returns>
        public string ToBase64(T entity)
        {

            MemoryStream memStream = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(memStream, entity);
            string result = Convert.ToBase64String(memStream.ToArray());
            return result;
        }

        #endregion
    }
}

   
  








Related examples in the same category

1.Double To Int 64 Bits, Int 64 Bits To Double
2.Base 64 encode
3.Base64 Encoder
4.Url Encode Base 64
5.The Base64 utility class performs base64 encoding and decoding.
6.The Base64 utility class performs base64 encoding and decoding. The resulting binary data is returned as an array of bytes.
7.Decodes all or part of the input base64 encoded StringBuffer
8.Encode To 64
9.Encrypt Key and Decrypt Key with Convert.ToBase64String and Convert.FromBase64String
10.Base64 Serializer
11.Base64 from http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java
12.Convert To Base 64 String
13.Get Xml value as Integer 64
14.Create a hex string from an Int64 value