Convenient parsing of XML nodes in C#

Hello everybody,

today I want to share with you code that I use in order to conveniently to parse different xml nodes in C#. 

Take a look at this class:

public static class XmlNodeProcessor
    {
        public static int? ParseInt(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            return int.Parse(s);
        }
 
        public static long? ParseLong(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            return long.Parse(s);
        }
 
        public static Guid? ParseGuid(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            return Guid.Parse(s);
        }
 
        public static long? ParseXmlNodeLong(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return node.Attributes[attributeName].Value.ParseLong();
        }
 
        public static Guid? ParseXmlNodeGuid(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return node.Attributes[attributeName].Value.ParseGuid();
        }
 
        public static short? ParseShort(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            return short.Parse(s);
        }
 
        public static decimal? ParseValue(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            return decimal.Parse(value);
        }
 
        public static DateTime? ParseDT(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            return DateTime.Parse(value);
        }
 
        public static DateTime? ParseXmlNodeDateTime(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return ParseDT(node.Attributes[attributeName].Value);
        }
 
        public static decimal? ParseXmlNodeDecimal(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return ParseValue(node.Attributes[attributeName].Value);
        }
 
        public static bool? ParseXmlNodeBit(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            var value = node.Attributes[attributeName].Value;
            if (value == "1")
            {
                return true;
            }
            return false;
        }
 
        public static string ParseXmlNodeString(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return node.Attributes[attributeName].Value;
        }
 
 
        public static int? ParseXmlNodeInt(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return node.Attributes[attributeName].Value.ParseInt();
        }
 
        public static short? ParseXmlNodeShort(this XmlNode node, string attributeName)
        {
            if (node == null)
            {
                return null;
            }
            if (node.Attributes[attributeName] == null)
            {
                return null;
            }
            return node.Attributes[attributeName].Value.ParseShort();
        }
 
    }

With such code you can parse easily nodes of XmlDocument with following lines:

int somevalue = nd.ParseXmlNodeInt("ErpLineItemCount") ?? 0;

No Comments

Add a Comment