Repository navigation
Expand file tree
/
Copy pathExtensions.cs
More file actions
70 lines (59 loc) · 2.16 KB
/
Copy pathExtensions.cs
File metadata and controls
70 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Xml;
namespace Schneegans.Unattend;
public static class Extensions
{
public static XmlNode SelectSingleNodeOrThrow(this XmlNode node, string xpath, XmlNamespaceManager nsmgr)
{
return node.SelectSingleNode(xpath, nsmgr) ?? throw new NullReferenceException($"No node matches XPath '{xpath}'.");
}
public static XmlNode SelectSingleNodeOrThrow(this XmlNode node, string xpath)
{
return node.SelectSingleNode(xpath) ?? throw new NullReferenceException($"No node matches XPath '{xpath}'.");
}
public static IEnumerable<XmlNode> SelectNodesOrEmpty(this XmlNode node, string xpath)
{
XmlNodeList? result = node.SelectNodes(xpath);
return result == null ? [] : result.Cast<XmlNode>();
}
public static IEnumerable<XmlNode> SelectNodesOrEmpty(this XmlNode node, string xpath, XmlNamespaceManager nsmgr)
{
XmlNodeList? result = node.SelectNodes(xpath, nsmgr);
return result == null ? [] : result.Cast<XmlNode>();
}
public static void RemoveSelf(this XmlNode node)
{
node.ParentNode!.RemoveChild(node);
}
public static void InsertTextWithIndentation(this XmlElement element, string text)
{
int level = element.SelectNodesOrEmpty("ancestor::*").Count();
element.AppendChild(element.OwnerDocument.CreateTextNode($"\r\n{text.Trim()}\r\n{new string('\t', level)}"));
}
public static IImmutableDictionary<string, T> ToKeyedDictionary<T>(this IEnumerable<T>? enumerable) where T : IKeyed
{
if (enumerable == null)
{
throw new NullReferenceException();
}
return enumerable.ToImmutableDictionary(
keySelector: value => value.Id,
keyComparer: StringComparer.OrdinalIgnoreCase
);
}
public static string JoinString<T>(this IEnumerable<T> enumerable, string separator)
{
return string.Join(separator, enumerable);
}
public static string JoinString<T>(this IEnumerable<T> enumerable, char separator)
{
return string.Join(separator, enumerable);
}
public static string JoinLines(this IEnumerable<string> enumerable)
{
return string.Join("\r\n", enumerable);
}
}