Thursday, February 27, 2014

Use extension methods for enum

Create below custom attribute class, which will be used  for enum.

public class EnumDescAttribute : Attribute
    {

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="EnumDescAttribute"/> class.
        /// </summary>
        /// <param name="description">The description.</param>
        public EnumDescAttribute(string description)
        {
            Description = description;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public string Description { get; set; }

        #endregion
    }

Sample Enum:

public enum QualifierType
    {
        [EnumDescAttribute("QQ")]
        SubAccount,
        [EnumDescAttribute("AA")]
        Account,
        [EnumDescAttribute("BB")]
        LegacyDivision,
        [EnumDescAttribute("CC")]
        DateRange
    }

Create below Extension class

public static class EnumExtensions
    {
        #region Public methods

        /// <summary>
        /// Gets the description attribute.
        /// </summary>
        /// <param name="en">The en.</param>
        /// <returns></returns>
        public static string GetAttributeValue(this Enum en)
        {
            string returnValue = en.ToString();
            Type type = en.GetType();

            //MemberInfo[] memInfo = type.GetMember(en.ToString());
            //if (memInfo != null && memInfo.Length > 0)
            //{
            //    object[] attributes = memInfo[0].GetCustomAttributes(typeof(EnumDescAttribute), false);

            //    if (attributes != null && attributes.Length > 0)
            //    {
            //        returnValue = ((EnumDescAttribute)attributes[0]).Description;
            //    }

            //}
            EnumDescAttribute attribute = GetAttribute<EnumDescAttribute>(en);
            if (attribute != null)
            {
                returnValue = attribute.Description;
            }
            return returnValue;
        }

        /// <summary>
        /// Converts to enum.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static T ConvertToEnum<T>(this string value)
        {
            T returnValue = default(T);

            returnValue = (T)Enum.Parse(typeof(T), value, true);

            return returnValue;
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Gets the attribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private static T GetAttribute<T>(this Enum value) where T : Attribute
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
         
            return type.GetField(name)
                        .GetCustomAttributes(false)
                        .OfType<T>()
                        .FirstOrDefault();
        }

        #endregion
    }


Get enum attribute value:

static void Main(string[] args)
        {
            Console.WriteLine(consts.Enumerations.QualifierType.Account.GetAttributeValue());
            Console.ReadLine();
         
            string value ="Account";
            Console.WriteLine(value.ConvertToEnum<consts.Enumerations.QualifierType>().ToString());
            Console.ReadLine();
        }

Wednesday, February 12, 2014

Update all items in a collection using Linq

collection = collection.Select(item => {item.PropertyToSet = value; return item;}).ToList();

or

users.ToList().ForEach(u =>
                      {
                         u.property1 = value1;
                         u.property2 = value2;
                      });

Got "Specified method is not supported" error when I try to use the xpathnavigator.setvalue()

This error occurred because of my XPathNavigator object created using XPathDocument object and it's read-only. So, XPathNavigator object has to be created form XMLDocument to edit the values using SetValue().


XPathDocument document = new XPathDocument(xmlStream);
XPathNavigator xPathNavigator = document.CreateNavigator();

Here we can get the "Specified method is not supported" error When I try to use the Setvalue() method.

To avoid this error, create navigator object using the XmlDocument .

XmlDocument doc = new XmlDocument();
doc.Load(@"{file-uri}"); 
XPathNavigator xPathNavigator = doc.CreateNavigator();

Friday, February 7, 2014

Get Unique values at document level using xpath


I have used preceding-sibling and following-sibling to get the unique skill values from the below XML message.

XmlDocument doc = new XmlDocument();
doc.Load(@"{file-uri}");

MemoryStream xmlStream = new MemoryStream();
            doc.Save(xmlStream);

            xmlStream.Flush();//Adjust this if you want read your data
            xmlStream.Position = 0;

            //XmlReader reader = XmlReader.Create(doc.OuterXml);
            //XPathDocument document = new XPathDocument(reader);
            XPathDocument document = new XPathDocument(xmlStream);
            XPathNavigator xPathNavigator = document.CreateNavigator();

            xPathNavigator.MoveToRoot(); // Move to the root element.
            if (xPathNavigator.HasChildren)
            {
                xPathNavigator.MoveToFirstChild();
            }
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
          nsmgr.AddNamespace("ns0:", "http://www.RVRCompany.com/en/core/employee");

XPathNodeIterator nodIterator = xPathNavigator.Select("//ns0:Employee/ns0:Project/ns0:Skills/ns0:Skill/ns0:SkillName[not(../../../../following-sibling::ns0:Employee/ns0:Project/ns0:Skills/ns0:Skill/ns0:SkillName = .) and not(preceding-sibling::ns0:SkillName=.)]",namespaceManager);

Console.WriteLine("Unique values **********************************************");
            while (nodIterator.MoveNext())
            {
                Console.WriteLine(nodIterator.Current.Value);
            }
            Console.ReadLine();

Sample XML:

<ns0:UpdateEmployeesRequest xmlns:ns0="http://www.RVRCompany.com/en/core/employee">
<ns0:EmployeeDetails>
<ns0:Employee>
<ns0:EmpID>1</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ1</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>2</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Java</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>3</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>VB6</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>SQL Server</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>IIS</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Linux</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>3</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>VB6</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>IIS</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Linux</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Tomcat</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
</ns0:EmployeeDetails>

</ns0:UpdateEmployeesRequest>