Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / XML

XML Serializer Library

Rate me:
Please Sign up or sign in to vote.
4.92/5 (12 votes)
10 Dec 2007CPOL4 min read 60.3K   1.8K   66  
A library for serializing any class in XML format.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.Collections.ObjectModel;

namespace Globe.Xml.Serialization
{
    /// <summary>
    /// Manages the serialization of objects.
    /// </summary>
    public class Serializer
    {
        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public Serializer()
        {
        }

        #endregion

        #region Properties

        XmlSerializeWriter _serializeWriter = new XmlSerializeWriter();
        /// <summary>
        /// Gets or sets the component used to write xml file.
        /// </summary>
        public XmlSerializeWriter SerializeWriter
        {
            get { return _serializeWriter; }
            set { _serializeWriter = value; }
        }

        XmlSerializeReader _serializeReader = new XmlSerializeReader();
        /// <summary>
        /// Gets or sets the component used to read xml file.
        /// </summary>
        public XmlSerializeReader SerializeReader
        {
            get { return _serializeReader; }
            set { _serializeReader = value; }
        }

        SerializableDataComposer _composer = new SerializableDataComposer();
        /// <summary>
        /// Gets or sets the component used to compose a SerialzableData from a serialzable object.
        /// </summary>
        public SerializableDataComposer Composer
        {
            get { return _composer; }
            set { _composer = value; }
        }

        SerializableDataDecomposer _decomposer = new SerializableDataDecomposer();
        /// <summary>
        /// Gets or sets the component used to decompose a serialzable object from a SerializableData.
        /// </summary>
        public SerializableDataDecomposer Decomposer
        {
            get { return _decomposer; }
            set { _decomposer = value; }
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Reset serializer state.
        /// </summary>
        virtual public void Reset()
        {
            _composer.SerializableDataInfo.Reset();
            _decomposer.SerializableDataInfo.Reset();
            _serializeReader.XmlDocument.RemoveAll();
            _serializeWriter.XmlDocument.RemoveAll();
        }

        /// <summary>
        /// Serialize an object.
        /// </summary>
        /// <param name="fileName">Filename.</param>
        /// <param name="data">Data to serialize.</param>
        virtual public void Serialize(string fileName, object data)
        {
            _decomposer.Decompose(data);
            _serializeWriter.WriteXml(fileName, _decomposer.SerializableDataInfo);
        }

        /// <summary>
        /// Deserialize an object.
        /// </summary>
        /// <param name="fileName">Filename.</param>
        /// <returns>Deserialized data.</returns>
        virtual public object Deserialize(string fileName)
        {
            _serializeReader.ReadXml(fileName, _decomposer.SerializableDataInfo);
            return _composer.Compose(_decomposer.SerializableDataInfo);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions