XmlExtensions.cs 1020 B

12345678910111213141516171819202122232425262728293031
  1. using System.Linq;
  2. using System.Xml;
  3. namespace Abp.Xml.Extensions
  4. {
  5. /// <summary>
  6. /// Extension methods for <see cref="XmlNode"/> class.
  7. /// </summary>
  8. public static class XmlNodeExtensions
  9. {
  10. /// <summary>
  11. /// Gets an attribute's value from an Xml node.
  12. /// </summary>
  13. /// <param name="node">The Xml node</param>
  14. /// <param name="attributeName">Attribute name</param>
  15. /// <returns>Value of the attribute</returns>
  16. public static string GetAttributeValueOrNull(this XmlNode node, string attributeName)
  17. {
  18. if (node.Attributes == null || node.Attributes.Count <= 0)
  19. {
  20. throw new AbpException(node.Name + " node has not " + attributeName + " attribute");
  21. }
  22. return node.Attributes
  23. .Cast<XmlAttribute>()
  24. .Where(attr => attr.Name == attributeName)
  25. .Select(attr => attr.Value)
  26. .FirstOrDefault();
  27. }
  28. }
  29. }