Parse an Xml element into a specified type. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Parse an Xml element into a specified type.

Demo Code


using System.Text;
using System.Reflection;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using System.Net;
using System.Drawing;
using System;/*from  w  w  w.j  a v a2s  . co  m*/

public class Main{
        /// <summary>
        /// Parse an Xml element into a specified type.
        /// </summary>
        /// <param name="elementNode">The xml node containing the element to parse.</param>
        /// <param name="elementType">The type of object to deserialize into.</param>
        /// <returns>The instance of the newly deserialized object.</returns>
        public static object ParseElement(XmlNode elementNode, Type elementType)
        {
            ConstructorInfo constructorInfo = elementType.GetConstructor(new Type[] { });
            object elementObject = constructorInfo.Invoke(null);

            XmlCollectionAttribute[] xmlCollectionClassAttributes = (XmlCollectionAttribute[])elementType.GetCustomAttributes(typeof(XmlCollectionAttribute), true);
            if (xmlCollectionClassAttributes.Length > 0)
            {
                if ((elementObject is IList == false))
                {
                    throw new Exception("Class marked as XmlCollection but type does not implement IList.");
                }

                XmlCollectionAttribute xmlCollectionClassAttribute = xmlCollectionClassAttributes[0];
                if ((elementNode.Name == xmlCollectionClassAttribute.CollectionElementName) && (elementNode.HasChildNodes))
                {
                    IList listObject = (IList)elementObject;
                    foreach (XmlNode childNode in elementNode.ChildNodes)
                    {
                        if (childNode.Name == xmlCollectionClassAttribute.ItemElementName)
                        {
                            object childValue = ParseElement(childNode, xmlCollectionClassAttribute.ItemType);
                            listObject.Add(childValue);
                        }
                    }
                }
            }
            else
            {
                PropertyInfo[] properties = elementType.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    Type propertyType = property.PropertyType;

                    XmlIgnoreAttribute[] ignoreAttributes = (XmlIgnoreAttribute[])property.GetCustomAttributes(typeof(XmlIgnoreAttribute), true);
                    if (ignoreAttributes.Length > 0)
                    {
                        continue;
                    }

                    bool isRequired;
                    bool invokeConstructor;
                    object defaultValue;
                    XmlRequiredAttribute[] xmlRequiredAttributes = (XmlRequiredAttribute[])property.GetCustomAttributes(typeof(XmlRequiredAttribute), true);
                    if (xmlRequiredAttributes.Length > 0)
                    {
                        XmlRequiredAttribute xmlRequiredAttribute = xmlRequiredAttributes[0];

                        isRequired = xmlRequiredAttribute.IsRequired;
                        invokeConstructor = xmlRequiredAttribute.InvokeConstructor;
                        defaultValue = xmlRequiredAttribute.DefaultValue;
                    }
                    else
                    {
                        isRequired = true;
                        invokeConstructor = false;
                        defaultValue = null;
                    }

                    object value;
                    XmlHasChildElementsAttribute[] xmlHasChildElementsAttributes = (XmlHasChildElementsAttribute[])propertyType.GetCustomAttributes(typeof(XmlHasChildElementsAttribute), true);
                    if ((xmlHasChildElementsAttributes.Length > 0) && (xmlHasChildElementsAttributes[0].HasChildElements))
                    {
                        XmlElementNameAttribute[] xmlElementNameAttributes = (XmlElementNameAttribute[])propertyType.GetCustomAttributes(typeof(XmlElementNameAttribute), true);
                        string childElementName = (xmlElementNameAttributes.Length > 0 ? xmlElementNameAttributes[0].XmlElementName : property.Name);
                        XmlNode childElementNode = XmlHelper.GetChildNode(elementNode, childElementName);

                        if (childElementNode != null)
                        {
                            value = ParseElement(childElementNode, propertyType);
                        }
                        else
                        {
                            if (isRequired)
                            {
                                throw new Exception("Child element [" + childElementName + "] not found.");
                            }

                            if (invokeConstructor)
                            {
                                ConstructorInfo childElementConstructorInfo = propertyType.GetConstructor(new Type[] { });
                                value = childElementConstructorInfo.Invoke(null);
                            }
                            else
                            {
                                value = defaultValue;
                            }
                        }
                    }
                    else
                    {
                        XmlAttributeNameAttribute[] xmlAttributeNameAttributes = (XmlAttributeNameAttribute[])property.GetCustomAttributes(typeof(XmlAttributeNameAttribute), true);
                        string attributeName = (xmlAttributeNameAttributes.Length > 0 ? xmlAttributeNameAttributes[0].XmlAttributeName : property.Name);
                        value = XmlHelper.GetAttribute(elementNode, attributeName, propertyType, isRequired, defaultValue);

                        if ((value == defaultValue) && (isRequired == false) && (invokeConstructor))
                        {
                            if (propertyType.IsValueType)
                            {
                                value = Activator.CreateInstance(propertyType);
                            }
                            else
                            {
                                ConstructorInfo propertyConstructorInfo = propertyType.GetConstructor(new Type[] { });
                                value = propertyConstructorInfo.Invoke(null);
                            }
                        }
                    }

                    XmlCollectionAttribute[] xmlCollectionAttributes = (XmlCollectionAttribute[])property.GetCustomAttributes(typeof(XmlCollectionAttribute), true);
                    if (xmlCollectionAttributes.Length > 0)
                    {
                        if ((value is IList == false))
                        {
                            throw new Exception("Property marked as XmlCollection but type does not implement IList.");
                        }

                        XmlCollectionAttribute xmlCollectionAttribute = xmlCollectionAttributes[0];
                        XmlNode xmlCollectionNode = XmlHelper.GetChildNode(elementNode, xmlCollectionAttribute.CollectionElementName);
                        if ((xmlCollectionNode != null) && (xmlCollectionNode.HasChildNodes))
                        {
                            IList listValue = (IList)value;
                            foreach (XmlNode childNode in xmlCollectionNode.ChildNodes)
                            {
                                if (childNode.Name == xmlCollectionAttribute.ItemElementName)
                                {
                                    object childValue = ParseElement(childNode, xmlCollectionAttribute.ItemType);
                                    listValue.Add(childValue);
                                }
                            }
                        }
                    }

                    property.SetValue(elementObject, value, null);
                }
            }

            return elementObject;
        }
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="xmlNode"></param>
      /// <param name="attributeName"></param>
      /// <param name="required"></param>
      /// <param name="defaultValue"></param>
      /// <returns></returns>
      public static T GetAttribute<T>(XmlNode xmlNode, string attributeName, bool required, T defaultValue)
      {
         T value;

         XmlNode foundNode = xmlNode.Attributes.GetNamedItem(attributeName);
         if (foundNode == null)
         {
            if (required)
            {
               throw new Exception("Required attribute [" + attributeName + "] not found");
            }

            value = defaultValue;
         }
         else
         {
            value = (T)GetValueFromString(typeof(T), foundNode.Value.Trim());
         }

         return value;
      }
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="xmlNode"></param>
      /// <param name="attributeName"></param>
      /// <returns></returns>
      public static T GetAttribute<T>(XmlNode xmlNode, string attributeName)
      {
         return GetAttribute<T>(xmlNode, attributeName, true, default(T));
      }
        /// <summary>
      /// 
      /// </summary>
      /// <param name="xmlNode"></param>
      /// <param name="attributeName"></param>
      /// <param name="type"></param>
      /// <param name="required"></param>
      /// <param name="defaultValue"></param>
      /// <returns></returns>
      public static object GetAttribute(XmlNode xmlNode, string attributeName, Type type, bool required, object defaultValue)
      {
         object value;

         XmlNode foundNode = xmlNode.Attributes.GetNamedItem(attributeName);
         if (foundNode == null)
         {
            if (required)
            {
               throw new Exception("Required attribute [" + attributeName + "] not found");
            }

                if ((defaultValue != null) && (defaultValue.GetType() != type))
            {
               throw new Exception("Default value specified with incompatible type.");
            }

            value = defaultValue;
         }
         else
         {
            value = GetValueFromString(type, foundNode.Value.Trim());
         }

         return value;
      }
        /// <summary>
      /// 
      /// </summary>
      /// <param name="xmlNode"></param>
      /// <param name="attributeName"></param>
      /// <param name="type"></param>
      /// <returns></returns>
      public static object GetAttribute(XmlNode xmlNode, string attributeName, Type type)
      {
         return GetAttribute(xmlNode, attributeName, type, true, null);
      }
        public static XmlNode GetChildNode(XmlNode parentNode, string childNodeName)
      {
         XmlNode foundChildNode = null;

         if (parentNode.HasChildNodes)
         {
            foreach (XmlNode childNode in parentNode.ChildNodes)
            {
               if (childNode.Name == childNodeName)
               {
                  foundChildNode = childNode;
                  break;
               }
            }
         }

         return foundChildNode;
      }
}

Related Tutorials