Click here to Skip to main content
15,893,668 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 516.5K   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>
    /// Creates a comment node for the main XML element.
    /// This attribute is applicable to classes only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class YAXCommentAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the comment.
        /// </summary>
        /// <value>The comment.</value>
        public string Comment { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXCommentAttribute"/> class.
        /// </summary>
        /// <param name="comment">The comment.</param>
        public YAXCommentAttribute(string comment)
        {
            this.Comment = comment;
        }
    }


    /// <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 | AttributeTargets.Struct)]
    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>
        /// Gets or sets a value indicating whether white space characters are to be
        /// treated as sparators or not.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if white space separator characters are to be
        /// treated as sparators; otherwise, <c>false</c>.
        /// </value>
        public bool IsWhiteSpaceSeparator { 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;
            IsWhiteSpaceSeparator = true;
        }
    }

    /// <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>
    /// Controls the serialization of generic Dictionary instances.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXDictionaryAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the alias for the key part of the dicitonary.
        /// </summary>
        /// <value></value>
        public string KeyName { get; set; }

        /// <summary>
        /// Gets or sets alias for the value part of the dicitonary.
        /// </summary>
        /// <value></value>
        public string ValueName { get; set; }

        /// <summary>
        /// Gets or sets alias for the element containing the Key-Value pair.
        /// </summary>
        /// <value></value>
        public string EachPairName { get; set; }

        /// <summary>
        /// Gets or sets the node type according to which the key part of the dictionary is serialized.
        /// </summary>
        /// <value></value>
        public YAXNodeTypes SerializeKeyAs { get; set; }

        /// <summary>
        /// Gets or sets the node type according to which the value part of the dictionary is serialized.
        /// </summary>
        /// <value></value>
        public YAXNodeTypes SerializeValueAs { get; set; }

        /// <summary>
        /// Gets or sets the key format string.
        /// </summary>
        /// <value></value>
        public string KeyFormatString { get; set; }

        /// <summary>
        /// Gets or sets the value format string.
        /// </summary>
        /// <value></value>
        public string ValueFormatString { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXDictionaryAttribute"/> class.
        /// </summary>
        public YAXDictionaryAttribute()
        {
            KeyName = null;
            ValueName = null;
            EachPairName = null;
            SerializeKeyAs = YAXNodeTypes.Element;
            SerializeValueAs = YAXNodeTypes.Element;
            KeyFormatString = null;
            ValueFormatString = null;
        }
    }

    /// <summary>
    /// Enumerates possible XML node types upon which a property can be serialized.
    /// </summary>
    public enum YAXNodeTypes
    {
        /// <summary>
        /// Serialize data as an attribute for the base element
        /// </summary>
        Attribute,

        /// <summary>
        /// Serialize data as an element
        /// </summary>
        Element
    }

    /// <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;
        }
    }

    /// <summary>
    /// Specifies the format string provided for serializing data. The format string is the parameter 
    /// passed to the <c>ToString</c> method.
    /// If this attribute is applied to collection classes, the format, therefore, is applied to 
    /// the collection members.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXFormatAttribute : System.Attribute
    {
        /// <summary>
        /// Gets or sets the format string needed to serialize data. The format string is the parameter 
        /// passed to the <c>ToString</c> method.
        /// </summary>
        /// <value></value>
        public string Format { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="YAXFormatAttribute"/> class.
        /// </summary>
        /// <param name="format">The format string.</param>
        public YAXFormatAttribute(string format)
        {
            this.Format = format;
        }
    }

    /// <summary>
    /// Specifies that a particular class that is driven from IEnumerable should not be treated
    /// as a collection class.
    /// This attribute is applicable to properties only.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class YAXNotCollectionAttribute : System.Attribute
    {
    }
}

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