ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

TryGetAttribute

Try/Get pattern for XDocument attributes

Source

public static bool TryGetAttribute(this XElement element, XName attributeName, out XAttribute attribute)
{          
     attribute = element.Attribute(attributeName);
     return (attribute != null);
}

Example

XAttribute cultureAttribute;
                if (element.TryGetAttribute("Culture", out cultureAttribute))
                {
//Do something with the attribute here
}

Author: Mike Cales

Submitted on: 18 sep 2013

Language: C#

Type: System.Xml.Linq.XElement

Views: 3210