| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="XElement"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class XElementExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Prevents the specified <see cref="XAttribute"/> |
| | 10 | | /// from being added more than once. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="element">The element.</param> |
| | 13 | | /// <param name="attribute">The attribute.</param> |
| | 14 | | public static void AddOnce(this XElement? element, XAttribute? attribute) |
| 0 | 15 | | { |
| 0 | 16 | | if (element == null) return; |
| 0 | 17 | | if (attribute == null) return; |
| | 18 | |
|
| 0 | 19 | | if (element.HasElementName(attribute.Name)) return; |
| | 20 | |
|
| 0 | 21 | | element.Add(attribute); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the <see cref="XElement"/>. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="node">The node.</param> |
| | 28 | | /// <param name="name">The name.</param> |
| | 29 | | public static XElement? GetElement(this XNode? node, XName? name) => |
| 0 | 30 | | node is XElement element && element.Name == name ? element : null; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Determines whether the <see cref="XElement"/> |
| | 34 | | /// has the specified <see cref="XName"/>. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="element">The element.</param> |
| | 37 | | /// <param name="name">The name.</param> |
| | 38 | | /// <returns> |
| | 39 | | /// <c>true</c> when the element has the name; otherwise, <c>false</c>. |
| | 40 | | /// </returns> |
| 0 | 41 | | public static bool HasElementName(this XElement? element, XName? name) => element != null && element.Name == name; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Determines whether the <see cref="XNode"/> |
| | 45 | | /// has the specified <see cref="XName"/>. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="node">The node.</param> |
| | 48 | | /// <param name="name">The name.</param> |
| | 49 | | /// <returns> |
| | 50 | | /// <c>true</c> when the node has the name; otherwise, <c>false</c>. |
| | 51 | | /// </returns> |
| | 52 | | public static bool HasElementName(this XNode? node, XName? name) => |
| 0 | 53 | | node is XElement element && (element.Name == name); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Determines whether the specified node is <see cref="XElement"/>. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="node">The node.</param> |
| | 59 | | /// <returns> |
| | 60 | | /// <c>true</c> if the specified node is element; otherwise, <c>false</c>. |
| | 61 | | /// </returns> |
| 0 | 62 | | public static bool IsElement(this XNode? node) => node is XElement; |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Converts the <see cref="XElement" /> into a attribute value or default. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="element">The element.</param> |
| | 68 | | /// <param name="attributeName">Name of the attribute.</param> |
| | 69 | | /// <param name="defaultValue">The default value.</param> |
| | 70 | | public static string? ToAttributeValueOrDefault(this XElement? element, string? attributeName, string? defaultValue) |
| 0 | 71 | | { |
| 0 | 72 | | var s = element.ToAttributeValueOrNull(attributeName); |
| | 73 | |
|
| 0 | 74 | | return string.IsNullOrWhiteSpace(s) ? defaultValue : s; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Returns the attribute value or null. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="element">The element.</param> |
| | 81 | | /// <param name="attributeName">Name of the attribute.</param> |
| | 82 | | public static string? ToAttributeValueOrNull(this XElement? element, string? attributeName) |
| 59976 | 83 | | { |
| 59976 | 84 | | if (element == null) return null; |
| 59976 | 85 | | if (attributeName == null) return null; |
| | 86 | |
|
| 59976 | 87 | | string? s = null; |
| 59976 | 88 | | var attr = element.Attribute(attributeName); |
| 85090 | 89 | | if (attr != null) s = attr.Value; |
| | 90 | |
|
| 59976 | 91 | | return string.IsNullOrWhiteSpace(s) ? null : s; |
| 59976 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Converts the <see cref="XElement" /> into a element value or default. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="element">The element.</param> |
| | 98 | | /// <param name="defaultValue">The default value.</param> |
| | 99 | | public static string? ToElementValueOrDefault(this XElement? element, string? defaultValue) |
| 0 | 100 | | { |
| 0 | 101 | | var s = element.ToElementValueOrNull(); |
| | 102 | |
|
| 0 | 103 | | return string.IsNullOrWhiteSpace(s) ? defaultValue : s; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Converts the <see cref="XElement" /> into a element value or null. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="element">The element.</param> |
| | 110 | | public static string? ToElementValueOrNull(this XElement? element) => |
| 25 | 111 | | string.IsNullOrWhiteSpace(element?.Value) ? null : element.Value; |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Returns the element value or null. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="elements">The elements.</param> |
| | 117 | | public static string? ToElementValueOrNull(this IEnumerable<XElement>? elements) => |
| 25 | 118 | | elements?.FirstOrDefault().ToElementValueOrNull(); |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Returns the specified <see cref="XElement"/> |
| | 122 | | /// without namespace qualifiers on elements and attributes. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="element">The element</param> |
| | 125 | | /// <remarks> |
| | 126 | | /// Based on “Answer: How to remove all namespaces from XML with C#?” |
| | 127 | | /// [http://stackoverflow.com/a/7238007/22944?stw=2] |
| | 128 | | /// </remarks> |
| | 129 | | public static XElement? WithoutNamespaces(this XElement? element) |
| 0 | 130 | | { |
| 0 | 131 | | if (element == null) return null; |
| | 132 | |
|
| | 133 | | #region delegates: |
| | 134 | |
|
| 0 | 135 | | XNode? GetChildNode(XNode e) => e.NodeType == XmlNodeType.Element ? (e as XElement).WithoutNamespaces() : e; |
| | 136 | |
|
| | 137 | | IEnumerable<XAttribute> GetAttributes(XElement e) => |
| 0 | 138 | | e.HasAttributes |
| 0 | 139 | | ? e.Attributes() |
| 0 | 140 | | .Where(a => !a.IsNamespaceDeclaration) |
| 0 | 141 | | .Select(a => new XAttribute(a.Name.LocalName, a.Value)) |
| 0 | 142 | | : Enumerable.Empty<XAttribute>(); |
| | 143 | |
|
| | 144 | | #endregion |
| | 145 | |
|
| 0 | 146 | | return new XElement(element.Name.LocalName, element.Nodes().Select(GetChildNode), GetAttributes(element)); |
| 0 | 147 | | } |
| | 148 | | } |