Click here to Skip to main content
15,885,164 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.8K   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>
    /// Makes a property to appear as an attribute for the class (i.e. the parent element) if possible.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXAttributeForClassAttribute : System.Attribute
    {
    }

    /// <summary>
    /// Makes a property to appear as an attribute for another element, if possible.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXAttributeForAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the element of which the property becomes an attribute.
        /// </summary>
        public string Parent { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXAttributeForAttribute"/> class.
        /// </summary>
        /// <param name="parent">The element of which the property becomes an attribute.</param>
        public YAXAttributeForAttribute(string parent)
        {
            this.Parent = parent;
        }
    }

    /// <summary>
    /// Prevents serialization of some property.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXDontSerializeAttribute : System.Attribute
    {
    }

    /// <summary>
    /// Defines an alias for the property under which the property will be serialized.
    /// This attribute is applicable to properties and classes only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
    public class YAXSerializeAsAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the alias for the property under which the property will be serialized.
        /// </summary>
        public string SerializeAs { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXSerializeAsAttribute"/> class.
        /// </summary>
        /// <param name="serializeAs">the alias for the property under which the property will be serialized.</param>
        public YAXSerializeAsAttribute(string serializeAs)
        {
            this.SerializeAs = serializeAs;
        }
    }

    /// <summary>
    /// Makes a property to appear as a child element for another element.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXElementForAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the element of which the property becomes a child element.
        /// </summary>
        /// <value>The element of which the property becomes a child element.</value>
        public string Parent { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXElementForAttribute"/> class.
        /// </summary>
        /// <param name="parent">The element of which the property becomes a child element.</param>
        public YAXElementForAttribute(string parent)
        {
            this.Parent = parent;
        }
    }

    /// <summary>
    /// Controls the serialization of collection instances.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXCollectionAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the type of the serialization of the collection.
        /// </summary>
        /// <value>The type of the serialization of the collection.</value>
        public YAXCollectionSerializationTypes SerializationType { get; set; }

        /// <summary>
        /// Gets or sets the string to separate collection items, if the Serialization type is set to <c>Serially</c>.
        /// </summary>
        /// <value>the string to separate collection items, if the Serialization Type is set to <c>Serially</c>.</value>
        public string SeparateBy { get; set; }

        /// <summary>
        /// Gets or sets the name of each child element corresponding to the collection members, if the Serialization type is set to <c>Recursive</c>.
        /// </summary>
        /// <value>The name of each child element corresponding to the collection members, if the Serialization type is set to <c>Recursive</c>.</value>
        public string EachElementName { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXCollectionAttribute"/> class.
        /// </summary>
        /// <param name="serType">type of the serialization of the collection.</param>
        public YAXCollectionAttribute(YAXCollectionSerializationTypes serType)
        {
            SerializationType = serType;
            SeparateBy = " ";
            EachElementName = null;
        }
    }

    /// <summary>
    /// Enumerates the possible ways of serializing collection classes
    /// </summary>
    public enum YAXCollectionSerializationTypes
    {
        /// <summary>
        /// Serializes each member of the collection, as a separate element.
        /// </summary>
        Recursive,
        /// <summary>
        /// Serializes all members of the collection in one element separated by some delimiter, if possible.
        /// </summary>
        Serially
    }

    /// <summary>
    /// Specifies the behaviour of the deserialization method, if the element/attribute corresponding to this property is missed in the XML input.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXErrorIfMissedAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the value indicating this situation is going to be treated as Error or Warning.
        /// </summary>
        /// <value>The value indicating this situation is going to be treated as Error or Warning.</value>
        public YAXExceptionTypes TreatAs { get; set; }

        /// <summary>
        /// Gets or sets the default value for the property if the element/attribute corresponding to this property is missed in the XML input.
        /// Setting <c>null</c> means do nothing.
        /// </summary>
        /// <value>The default value.</value>
        public object DefaultValue { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXErrorIfMissedAttribute"/> class.
        /// </summary>
        /// <param name="treatAs">The value indicating this situation is going to be treated as Error or Warning.</param>
        public YAXErrorIfMissedAttribute(YAXExceptionTypes treatAs)
        {
            this.TreatAs = treatAs;
            DefaultValue = null;
        }
    }
}

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