Click here to Skip to main content
15,884,709 members
Articles / Programming Languages / XML

Yet Another XML Serialization Library for the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (91 votes)
2 Oct 2012MIT24 min read 509.6K   207  
A flexible XML serialization library that lets developers design the XML file structure, and select the exception handling policy. YAXLib supports polymorphic serialization and serializing generic and non-generic collection classes and arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YAXLib
{
    /// <summary>
    /// The base for all exception classes of YAXLib
    /// </summary>
    public class YAXException : System.Exception
    {
    }

    /// <summary>
    /// Raised when trying to serialize an attribute where 
    /// another attribute with the same name already exists.
    /// This exception is raised during serialization.
    /// </summary>
    public class YAXAttributeAlreadyExistsException : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the attribute.
        /// </summary>
        /// <value>The name of the attribute.</value>
        public string AttrName { get; set; }
        
        /// <summary>
        /// Initializes a new instance of the <see cref="YAXAttributeAlreadyExistsException"/> class.
        /// </summary>
        /// <param name="attrName">Name of the attribute.</param>
        public YAXAttributeAlreadyExistsException(string attrName)
        {
            this.AttrName = attrName;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("An attribute with this name already exists: '{0}'.", AttrName);
            }
        }
    }

    /// <summary>
    /// Raised when the attribute corresponding to some property is not present in the given XML file, when deserializing.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXAttributeMissingException : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the attribute.
        /// </summary>
        /// <value>The name of the attribute.</value>
        public string AttributeName { get; set; }
        /// <summary>
        /// Initializes a new instance of the <see cref="YAXAttributeMissingException"/> class.
        /// </summary>
        /// <param name="attrName">Name of the attribute.</param>
        public YAXAttributeMissingException(string attrName)
        {
            this.AttributeName = attrName;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("No attributes with this name found: '{0}'.", AttributeName);
            }
        }
    }

    /// <summary>
    /// Raised when the element corresponding to some property is not present in the given XML file, when deserializing.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXElementMissingException : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the element.
        /// </summary>
        /// <value>The name of the element.</value>
        public string ElementName { get; set; }
        /// <summary>
        /// Initializes a new instance of the <see cref="YAXElementMissingException"/> class.
        /// </summary>
        /// <param name="elemName">Name of the element.</param>
        public YAXElementMissingException(string elemName)
        {
            this.ElementName = elemName;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("No elements with this name found: '{0}'.", ElementName);
            }
        }
    }

    /// <summary>
    /// Raised when the value provided for some property in the XML input, cannot be 
    /// converted to the type of the property.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXBadlyFormedInput : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the element.
        /// </summary>
        /// <value>The name of the element.</value>
        public string ElementName { get; set; }

        /// <summary>
        /// Gets or sets the value of the input which could not be converted to the type of the property.
        /// </summary>
        /// <value>The value of the input which could not be converted to the type of the property.</value>
        public string BadInput { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXBadlyFormedInput"/> class.
        /// </summary>
        /// <param name="elemName">Name of the element.</param>
        /// <param name="badInput">The value of the input which could not be converted to the type of the property.</param>
        public YAXBadlyFormedInput(string elemName, string badInput)
        {
            this.ElementName = elemName;
            this.BadInput = badInput;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("The format of the value specified for the property '{0}' is not proper: '{1}'.", ElementName, BadInput);
            }
        }
    }

    /// <summary>
    /// Raised when the value provided for some property in the XML input, cannot be 
    /// assigned to the property.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXPropertyCannotBeAssignedTo : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        /// <value>The name of the property.</value>
        public string PropertyName { get; set; }
        /// <summary>
        /// Initializes a new instance of the <see cref="YAXPropertyCannotBeAssignedTo"/> class.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        public YAXPropertyCannotBeAssignedTo(string propName)
        {
            this.PropertyName = propName;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("Could not assign to the property '{0}'.", PropertyName);
            }
        }
    }

    /// <summary>
    /// Raised when some member of the collection in the input XML, cannot be added to the collection object.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXCannotAddObjectToCollection : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        /// <value>The name of the property.</value>
        public string PropertyName { get; set; }

        /// <summary>
        /// Gets or sets the object that could not be added to the collection.
        /// </summary>
        /// <value>the object that could not be added to the collection.</value>
        public object ObjectToAdd { get; set; }
        /// <summary>
        /// Initializes a new instance of the <see cref="YAXCannotAddObjectToCollection"/> class.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        /// <param name="obj">The object that could not be added to the collection.</param>
        public YAXCannotAddObjectToCollection(string propName, object obj)
        {
            this.PropertyName = propName;
            this.ObjectToAdd = obj;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("Could not add object ('{0}') to the collection ('{1}').", ObjectToAdd, PropertyName);
            }
        }
    }

    /// <summary>
    /// Raised when the default value specified by the <c>YAXErrorIfMissedAttribute</c> could not be assigned to the property.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXDefaultValueCannotBeAssigned : YAXException
    {
        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        /// <value>The name of the property.</value>
        public string PropertyName { get; set; }

        /// <summary>
        /// Gets or sets the default value which caused the problem.
        /// </summary>
        /// <value>The default value which caused the problem.</value>
        public object TheDefaultValue { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXDefaultValueCannotBeAssigned"/> class.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        /// <param name="defaultValue">The default value which caused the problem.</param>
        public YAXDefaultValueCannotBeAssigned(string propName, object defaultValue)
        {
            this.PropertyName = propName;
            this.TheDefaultValue = defaultValue;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("Could not assign the default value specified ('{0}') for the property '{1}'.", TheDefaultValue, PropertyName);
            }
        }
    }

    /// <summary>
    /// Raised when the XML input does not follow standard XML formatting rules.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXBadlyFormedXML : YAXException
    {
        Exception m_innerException = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXBadlyFormedXML"/> class.
        /// </summary>
        public YAXBadlyFormedXML()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXBadlyFormedXML"/> class.
        /// </summary>
        /// <param name="innerException">The inner exception.</param>
        public YAXBadlyFormedXML(Exception innerException) 
        {
            m_innerException = innerException;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                string msg = String.Format("The input xml file is not properly formatted!");
                if (m_innerException != null)
                    msg += String.Format("\r\nMore Details:\r\n{0}", m_innerException.Message);
                return msg;
            }
        }
    }

    /// <summary>
    /// Raised when trying to serialize self-referential types. This exception cannot be turned off.
    /// This exception is raised during deserialization.
    /// </summary>
    public class YAXCannotSerializeSelfReferentialTypes : YAXException
    {
        /// <summary>
        /// Gets or sets the self-referential type that caused the problem.
        /// </summary>
        /// <value>The type of the self-referential type that caused the problem.</value>
        public Type SelfReferentialType { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXCannotSerializeSelfReferentialTypes"/> class.
        /// </summary>
        /// <param name="type">The the self-referential type that caused the problem.</param>
        public YAXCannotSerializeSelfReferentialTypes(Type type)
        {
            this.SelfReferentialType = type;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("Self Referential types ('{0}') cannot be serialized.", SelfReferentialType.FullName);
            }
        }
    }

    /// <summary>
    /// Raised when the object provided for serialization is not of the type provided for the serializer. This exception cannot be turned off.
    /// This exception is raised during serialization.
    /// </summary>
    public class YAXObjectTypeMismatch : YAXException
    {
        /// <summary>
        /// Gets or sets the expected type.
        /// </summary>
        /// <value>The expected type.</value>
        public Type ExpectedType { get; set; }

        /// <summary>
        /// Gets or sets the type of the object which did not match the expected type.
        /// </summary>
        /// <value>The type of the object which did not match the expected type.</value>
        public Type ReceivedType { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXObjectTypeMismatch"/> class.
        /// </summary>
        /// <param name="expectedType">The expected type.</param>
        /// <param name="receivedType">The type of the object which did not match the expected type.</param>
        public YAXObjectTypeMismatch(Type expectedType, Type receivedType)
        {
            this.ExpectedType = expectedType;
            this.ReceivedType = receivedType;
        }

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The error message that explains the reason for the exception, or an empty string("").
        /// </returns>
        public override string Message
        {
            get
            {
                return String.Format("Expected an object of type '{0}' but received an object of type '{1}'.", 
                    ExpectedType.Name, ReceivedType.Name);
            }
        }
    }
}

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 MIT License


Written By
Software Developer
Australia Australia
A software designer and developer

Comments and Discussions