-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathExtensions.cs
59 lines (50 loc) · 1.77 KB
/
Extensions.cs
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
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 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);
}
}