Click here to Skip to main content
15,885,720 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 510.2K   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;
using YAXLib;
using System.Collections;

namespace DemoApplication.SampleClasses
{
    [YAXComment(@"This example tries to show almost all features of YAXLib which were not shown before.
      FamousPoints - shows a dictionary with a non-primitive value member.
      IntEnumerable - shows serializing properties of type IEnumerable<>
      Students - shows the usage of YAXNotCollection attribute")]
    public class MoreComplexExample
    {
        [YAXDictionary(EachPairName = "PointInfo", KeyName = "PName",
            ValueName = "ThePoint", SerializeKeyAs = YAXNodeTypes.Attribute,
            SerializeValueAs = YAXNodeTypes.Attribute)]
        public Dictionary<string, MyPoint> FamousPoints { get; set; }

        private List<int> m_lst = new List<int>();

        public IEnumerable<int> IntEnumerable
        {
            get
            {
                return m_lst;
            }

            set
            {
                m_lst = value.ToList();
            }
        }

        [YAXNotCollection]
        public Students Students { get; set; }

        public override string ToString()
        {
            return GeneralToStringProvider.GeneralToString(this);
        }

        public static MoreComplexExample GetSampleInstance()
        {
            Dictionary<string, MyPoint> famPoints = new Dictionary<string, MyPoint>();
            famPoints.Add("Center", new MyPoint() { X = 0, Y = 0 });
            famPoints.Add("Q1", new MyPoint() { X = 1, Y = 1 });
            famPoints.Add("Q2", new MyPoint() { X = -1, Y = 1 });

            int[] nArray = new int[] { 1, 3, 5, 7 };

            return new MoreComplexExample()
            {
                FamousPoints = famPoints,
                Students = Students.GetSampleInstance(),
                IntEnumerable = nArray
            };
        }
    }

    public class MyPoint
    {
        [YAXAttributeForClass]
        public int X { get; set; }

        [YAXAttributeForClass]
        public int Y { get; set; }

        public override string ToString()
        {
            return String.Format("({0}, {1})", X, Y);
        }
    }

    public class Students : IEnumerable<string>
    {
        public int Count { get; set; }

        public string[] Names { get; set; }

        public string[] Families { get; set; }

        public string GetAt(int i)
        {
            return String.Format("{0}, {1}", Families[i], Names[i]);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(String.Format("Count = {0}", Count));
            sb.AppendLine(String.Format("Names: "));
            foreach (string str in Names)
                sb.Append(str + "  ");

            sb.AppendLine(String.Format("Families: "));
            foreach (string str in Families)
                sb.Append(str + "  ");

            return sb.ToString();
        }

        public static Students GetSampleInstance()
        {
            return new Students()
            {
                Count = 3,
                Names = new string[] { "Ali", "Dave", "John" },
                Families = new string[] { "Alavi", "Black", "Doe" }
            };
        }

        #region IEnumerable<string> Members

        public IEnumerator<string> GetEnumerator()
        {
            return new StudentsEnumerator(this);
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new StudentsEnumerator(this);
        }

        #endregion
    }

    public class StudentsEnumerator : IEnumerator<string>
    {
        Students m_students = null;
        int counter = -1;
        public StudentsEnumerator(Students studentsInstance)
        {
            m_students = studentsInstance;
            counter = -1;
        }

        #region IEnumerator<string> Members

        public string Current
        {
            get { return m_students.GetAt(counter); }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {

        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { return m_students.GetAt(counter); }
        }

        public bool MoveNext()
        {
            counter++;
            if (counter >= m_students.Count)
                return false;
            return true;
        }

        public void Reset()
        {
            counter = -1;
        }

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


Written By
Software Developer
Australia Australia
A software designer and developer

Comments and Discussions