Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / XML

XML Serialization of Complex .NET Objects

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
19 Oct 2008CPOL17 min read 130.9K   2.6K   61  
Yet another custom XML serializer, with a slightly different approach.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace CommonLibrary
{
    public abstract class CustomXmlSerializerBase
    {
        static Dictionary<string, IDictionary<string, FieldInfo>> fieldInfoCache = new Dictionary<string, IDictionary<string, FieldInfo>>();        

        protected XmlDocument doc = new XmlDocument();

        protected static IDictionary<string, FieldInfo> GetTypeFieldInfo(Type objType)
        {
            string typeName = objType.FullName;
            IDictionary<string, FieldInfo> fields;
            if (!fieldInfoCache.TryGetValue(typeName, out fields))
            {
                // fetch fields
                FieldInfo[] fieldInfo = objType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
                                                          BindingFlags.Public | BindingFlags.DeclaredOnly);

                Dictionary<string, FieldInfo> dict = new Dictionary<string, FieldInfo>(fieldInfo.Length);
                foreach (FieldInfo field in fieldInfo)
                {
                    if (!field.FieldType.IsSubclassOf(typeof(MulticastDelegate)))
                    {
                        object[] attribs = field.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
                        if (attribs.Length == 0)
                        {
                            dict.Add(field.Name, field);
                        }
                    }
                }

                // check base class as well
                Type baseType = objType.BaseType;
                if (baseType != null && baseType != typeof(object))
                {
                    // should we include this base class?
                    object[] attribs = baseType.GetCustomAttributes(typeof(XmlIgnoreBaseTypeAttribute), false);
                    if (attribs.Length == 0)
                    {
                        IDictionary<string, FieldInfo> baseFields = GetTypeFieldInfo(baseType);
                        // add fields
                        foreach (KeyValuePair<string, FieldInfo> kv in baseFields)
                        {
                            string key = kv.Key;
                            if (dict.ContainsKey(key))
                            {
                                // make field name unique
                                key = "base." + key;
                            }
                            dict.Add(key, kv.Value);
                        }
                    }
                }

                fields = dict;
                fieldInfoCache.Add(typeName, fields);
            }
            return fields;
        }

        protected class TypeInfo
        {
            internal int TypeId;
            internal XmlElement OnlyElement;

            internal void WriteTypeId(XmlElement element)
            {
                element.SetAttribute("typeid", TypeId.ToString());
            }
        }        
    }
}

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
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions