Xml Serialization Manager : Xml Serialization « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » Xml SerializationScreenshots 
Xml Serialization Manager
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace SPAdvancedSearch.Utility
{
    /// <summary>
    /// Defines a class for helping with serializing and deserializing objects.
    /// </summary>
    public class SerializationManager
    {

        #region Methods

        public static string DehydrateObject(object objectToSerialize)
        {
            return DehydrateObject(objectToSerialize, null);
        }

        /// <summary>
        /// Returns the string representation of a serializable object.
        /// </summary>
        /// <param name="objectToSerialze"></param>
        /// <returns></returns>
        public static string DehydrateObject(object objectToSerialize, Type[] additionalTypes)
        {
            MemoryStream stm = new MemoryStream();
            StreamReader reader = new StreamReader(stm);

            string xml = string.Empty;

            try
            {
                SerializationManager.PersistObject(objectToSerialize, stm, additionalTypes);
                stm.Position = 0;
                xml = reader.ReadToEnd();
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Error returning the xml representation of {0}. -- {1}", objectToSerialize.GetType().ToString(), e.Message + ", " + e.StackTrace), e);
            }
            finally
            {
                reader.Close();
                stm.Close();
            }

            return xml;

        }

        public static object HydrateObjectFromXml(Type type, string xml)
        {
            return HydrateObjectFromXml(type, xml, null);
        }

        /// <summary>
        /// Hydrates an object from an XML string.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object HydrateObjectFromXml(Type type, string xml, Type[] additionalTypes)
        {
            if (!string.IsNullOrEmpty(xml))
            {
                MemoryStream stm = new MemoryStream();
                StreamWriter writer = new StreamWriter(stm);

                try
                {
                    writer.AutoFlush = true;
                    writer.Write(xml);

                    return HydratePersistedObject(type, stm, additionalTypes);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("The XML could not be loaded.", e);
                }
                finally
                {
                    writer.Close();
                    stm.Close();
                }
            }

            return null;

        }

        /// <summary>
        /// Hydrates a serialized object from a <see cref="MemoryStream"/>.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stm"></param>
        /// <returns></returns>
        private static object HydratePersistedObject(Type type, MemoryStream stm, Type[] additionalTypes)
        {
            XmlSerializer xmlSerializer = null;

            if (additionalTypes == null)
            {
                xmlSerializer = new XmlSerializer(type);
            }
            else
            {
                xmlSerializer = new XmlSerializer(type, additionalTypes);
            }

            try
            {
                stm.Position = 0;
                return xmlSerializer.Deserialize(stm);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error deserializing the object.", e);
            }

        }

        /// <summary>
        /// Serializes an object to a <see cref="MemoryStream"/>.
        /// </summary>
        /// <param name="objectToSerialze"></param>
        /// <param name="stm"></param>
        private static void PersistObject(object objectToSerialize, MemoryStream stm, Type[] additionalTypes)
        {
            XmlSerializer xmlSerializer = null;
            if (additionalTypes == null)
            {
                xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
            }
            else
            {
                xmlSerializer = new XmlSerializer(objectToSerialize.GetType(), additionalTypes);
            }
            StreamWriter writer = new StreamWriter(stm);

            try
            {

                writer.AutoFlush = true;
                xmlSerializer.Serialize(writer, objectToSerialize);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error persisting the object.", e);
            }
        }

        #endregion

    }
}

   
    
  
Related examples in the same category
1.Serialize List of Objects
2.Use XmlSerializer
3.Set Xml Attribute when serilzation
4.Use XML Serialization with Custom Objects
5.XmlRootAttribute
6.Takes an XML file and exports the Object it holds
7.Converts an XML string to an object
8.Deserializes/Serializes an xml document back into an object
9.Deserialize with XmlSerializer
10.XmlSerializer Util
11.Serilize to Xml
12.Xml Serialization Helper
13.Xml Serialization Helper 2
14.Tiny Xml Serializer
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.