Find and get Attribute value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Collections;
public static class Utils
{
public static XAttribute FindAttribute(this XElement elem, string xpath)
{
return ((IEnumerable)elem.XPathEvaluate(xpath)).Cast<XAttribute>().FirstOrDefault();
}
public static int GetAttributeValueAsInt(this XAttribute elem)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
return int.Parse(elem.Value, ci.NumberFormat);
}
public static double GetAttributeValueAsDouble(this XAttribute elem)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
return double.Parse(elem.Value, ci.NumberFormat);
}
public static DateTime GetAttributeValueAsDateTime(this XAttribute elem)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
return DateTime.Parse(elem.Value, ci.DateTimeFormat);
}
public static int GetAttributeValueAsInt(this XElement elem, string xpath)
{
return elem.FindAttribute(xpath).GetAttributeValueAsInt();
}
public static double GetAttributeValueAsDouble(this XElement elem, string xpath)
{
return elem.FindAttribute(xpath).GetAttributeValueAsDouble();
}
public static DateTime GetAttributeValueAsDateTime(this XElement elem, string xpath)
{
return elem.FindAttribute(xpath).GetAttributeValueAsDateTime();
}
}
Related examples in the same category