-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPathExtensions.cs
104 lines (97 loc) · 4.39 KB
/
XPathExtensions.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright SeaRisen LLC
// You may use this code without restrictions, but keep the copyright notice with this code.
// This file is found at: https://github.com/ChuckSavage/XmlLib
// If you find this code helpful and would like to donate, please consider purchasing one of
// the products at http://products.searisen.com, thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using XmlLib.nXPath;
namespace XmlLib
{
public static class XPathExtensions
{
/// <summary>
/// Navigate to a specific path within source. (create path if it doesn't exist?)
/// <remarks>See XPath docs for help on using [number][key=value]
/// syntax (http://www.w3.org/TR/xpath/)</remarks>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException" />
public static IEnumerable<XElement> XPath(this XElement source, XPathString path, bool create)
{
return cXPath.Enumerable(source, path, create);
}
/// <summary>
/// Navigate to a specific path within source, create it if it doesn't exist.
/// <remarks>See XPath docs for help on using [number][key=value]
/// syntax (http://www.w3.org/TR/xpath/)</remarks>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException" />
public static IEnumerable<XElement> XPath(this XElement source, string path, params object[] args)
{
return XPath(source, new XPathString(path, args), false);
}
/// <summary>
/// Navigate to a specific path within source. (create path if it doesn't exist?)
/// <remarks>See XPath docs for help on using [number][key=value]
/// syntax (http://www.w3.org/TR/xpath/)</remarks>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException" />
public static XElement XPathElement(this XElement source, XPathString path, bool create)
{
return XPath(source, path, create).FirstOrDefault();
}
/// <summary>
/// Navigate to a specific path within source, create it if it doesn't exist.
/// <remarks>See XPath docs for help on using [number][key=value]
/// syntax (http://www.w3.org/TR/xpath/)</remarks>
/// </summary>
/// <exception cref="ArgumentOutOfRangeException" />
public static XElement XPathElement(this XElement source, string path, params object[] args)
{
return XPathElement(source, new XPathString(path, args), false);
}
/// <summary>
/// Generic Get for a type.
/// <remarks>
/// It works as long as there is a converter for the type to convert
/// from string.
/// </remarks>
/// </summary>
/// <returns>The elements converted to its type or the default if it
/// didn't exist or was empty.</returns>
public static IEnumerable<T> XGet<T>(this XElement source, string path, T @default, params object[] args)
{
XPathString xp = new XPathString(path, args);
if (xp.PathSegments.Length > 1)
{
XPathString last = xp.PathSegments.Last();
if (last.Text == last.Format && 0 == last.Values.Length && !last.IsXPath)
{
xp = XPathString.Combine(xp.PathSegments.Take(xp.PathSegments.Length - 1));
var results = XPath(source, xp, true);
return results.Select(x => x.Get(last.Text, @default));
}
}
return XPath(source, xp, true)
.Select(x => x.Get(null, @default));
}
/// <summary>
/// Generic Get for a type.
/// <remarks>
/// It works as long as there is a converter for the type to convert
/// from string.
/// </remarks>
/// </summary>
/// <returns>The element converted to its type or the default if it
/// didn't exist or was empty.</returns>
public static T XGetElement<T>(this XElement source, string path, T @default, params object[] args)
{
var results = XGet(source, path, @default, args);
if (results.Count() > 0)
return results.FirstOrDefault();
return @default;
}
}
}