/// <summary> /// Gets the value of the attribute with the specified local name and namespace URI. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="xelem">The element to act on.</param> /// <param name="attrLocalName">Name of the attribute local.</param> /// <param name="transformer">The transformer to/from T object.</param> /// <param name="defaultValue">The default value.</param> /// <returns> /// The attribute's value as a string. /// </returns> /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception> public static T Get <T>(this XElement xelem, string attrLocalName, IAttributeValueTransformer <T> transformer, T defaultValue) { if (transformer == null) { throw new ArgumentNullException(nameof(transformer)); } return(xelem.HasAttribute(attrLocalName) ? transformer.ConvertFrom(xelem.Get(attrLocalName)) : defaultValue); }
/// <summary> /// Sets the value of attribute with the specified local name within the specified namespace. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="xelem">This <see cref="XmlElement"/> node to act on.</param> /// <param name="localName">The local name of the new element.</param> /// <param name="ns">The namespace.</param> /// <param name="value">The string value.</param> /// <param name="transformer">The transformer.</param> /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception> public static void PutAttribute <T>(this XElement xelem, string localName, XNamespace ns, T value, IAttributeValueTransformer <T> transformer) { if (transformer == null) { throw new ArgumentNullException(nameof(transformer)); } xelem.SetAttributeValue(ns + localName, transformer.ToString(value)); }
/// <summary> /// Gets the value of the attribute with the specified local name and namespace. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="xelem">The element to act on.</param> /// <param name="attrLocalName">Name of the attribute local.</param> /// <param name="ns">The namespace.</param> /// <param name="transformer">The transformer to/from T object.</param> /// <returns> /// The attribute's value as a string. /// </returns> /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception> public static T Get <T>(this XElement xelem, string attrLocalName, XNamespace ns, IAttributeValueTransformer <T> transformer) { if (transformer == null) { throw new ArgumentNullException(nameof(transformer)); } return(transformer.ConvertFrom(xelem.Get(attrLocalName, ns))); }