Extends XElement to Get Attribute Value - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Extends XElement to Get Attribute Value

Demo Code

// Copyright (c) Microsoft Corporation. All rights reserved. 
using System.Xml.Linq;
using System;/*w  w  w.  j a v a  2 s.c  o m*/

public class Main{
        /// <summary>
        /// Extends XElement 
        /// </summary>
        /// <param name="element">
        /// the xml node
        /// </param>
        /// <param name="attributeName">
        /// attribute name
        /// </param>
        /// <param name="defaultValue">
        /// default value
        /// </param>
        /// <returns>
        /// the attribute value
        /// </returns>
        public static long GetAttributeValue(this XElement element, string attributeName, long defaultValue)
        {
            XAttribute attribute = element.Attribute(attributeName);
            long value;
            return (attribute == null || !long.TryParse(attribute.Value, out value)) ? defaultValue : value;
        }
        /// <summary>
        /// Extends XElement 
        /// </summary>
        /// <param name="element">
        /// the xml node
        /// </param>
        /// <param name="attributeName">
        /// attribute name
        /// </param>
        /// <param name="defaultValue">
        /// default value
        /// </param>
        /// <returns>
        /// the attribute value
        /// </returns>
        public static DateTime GetAttributeValue(this XElement element, string attributeName, DateTime defaultValue)
        {
            XAttribute attribute = element.Attribute(attributeName);
            return (attribute == null) ? defaultValue : DateTime.Parse(attribute.Value);
        }
        /// <summary>
        /// Extends XElement 
        /// </summary>
        /// <param name="element">
        /// the xml node
        /// </param>
        /// <param name="attributeName">
        /// attribute name
        /// </param>
        /// <param name="defaultValue">
        /// default value
        /// </param>
        /// <returns>
        /// the attribute value
        /// </returns>
        public static Guid GetAttributeValue(this XElement element, string attributeName, Guid defaultValue)
        {
            XAttribute attribute = element.Attribute(attributeName);
            return (attribute == null) ? defaultValue : Guid.Parse(attribute.Value);
        }
        #region Public Methods and Operators

        /// <summary>
        /// Extends XElement 
        /// </summary>
        /// <param name="element">
        /// the xml node
        /// </param>
        /// <param name="attributeName">
        /// attribute name
        /// </param>
        /// <param name="defaultValue">
        /// default value
        /// </param>
        /// <returns>
        /// the attribute value
        /// </returns>
        public static string GetAttributeValue(this XElement element, string attributeName, string defaultValue)
        {
            XAttribute attribute = element.Attribute(attributeName);
            return (attribute == null) ? defaultValue : attribute.Value;
        }
}

Related Tutorials