| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="XNode"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class XNodeExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Gets the inner XML. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="node">The node.</param> |
| 0 | 12 | | public static string? GetInnerXml(this XNode node) => node.GetInnerXml(true, ReaderOptions.None); |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Gets the inner XML. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="node">The node.</param> |
| | 18 | | /// <param name="stripNamespaces">if set to <c>true</c> then strip namespaces (<c>true</c> by default).</param> |
| | 19 | | /// <param name="options">The options (<see cref="ReaderOptions.None"/> by default).</param> |
| | 20 | | /// <remarks> |
| | 21 | | /// “If performance is important (e.g. lots of XML, parsed frequently), I'd use Daniel's CreateReader method every t |
| | 22 | | /// [http://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement] |
| | 23 | | /// —Luke Sampson |
| | 24 | | /// </remarks> |
| | 25 | | public static string? GetInnerXml(this XNode? node, bool stripNamespaces, ReaderOptions options) |
| 0 | 26 | | { |
| 0 | 27 | | if (node == null) return null; |
| | 28 | |
|
| 0 | 29 | | var reader = node.CreateReader(options); |
| 0 | 30 | | reader.MoveToContent(); |
| 0 | 31 | | var innerXml = reader.ReadInnerXml(); |
| | 32 | |
|
| 0 | 33 | | if (stripNamespaces) innerXml = XmlUtility.StripNamespaces(innerXml); |
| | 34 | |
|
| 0 | 35 | | return innerXml; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets <see cref="IXmlNamespaceResolver"/> from the specified node. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="node">The node.</param> |
| | 42 | | public static IXmlNamespaceResolver? GetNamespaceResolver(this XNode? node) => |
| 0 | 43 | | node == null ? null : XmlUtility.GetNamespaceManager(node.CreateNavigator()); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets the outer XML. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="node">The node.</param> |
| 0 | 49 | | public static string? GetOuterXml(this XNode? node) => node?.GetOuterXml(true, ReaderOptions.None); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Gets the outer XML. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="node">The node.</param> |
| | 55 | | /// <param name="stripNamespaces">if set to <c>true</c> then strip namespaces (<c>true</c> by default).</param> |
| | 56 | | /// <param name="options">The options (<see cref="ReaderOptions.None"/> by default).</param> |
| | 57 | | public static string? GetOuterXml(this XNode? node, bool stripNamespaces, ReaderOptions options) |
| 0 | 58 | | { |
| 0 | 59 | | if (node == null) return null; |
| | 60 | |
|
| 0 | 61 | | var reader = node.CreateReader(options); |
| 0 | 62 | | reader.MoveToContent(); |
| 0 | 63 | | var outerXml = reader.ReadOuterXml(); |
| | 64 | |
|
| 0 | 65 | | if (stripNamespaces) outerXml = XmlUtility.StripNamespaces(outerXml); |
| | 66 | |
|
| 0 | 67 | | return outerXml; |
| 0 | 68 | | } |
| | 69 | | } |