forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXLinqExtensions.cs
More file actions
288 lines (252 loc) · 9.25 KB
/
XLinqExtensions.cs
File metadata and controls
288 lines (252 loc) · 9.25 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#if !SL5 && !IOS && !XBOX
//
// ServiceStack: Useful extensions to simplify parsing xml with XLinq
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2014 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of reddis and ServiceStack: new BSD license.
//
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
namespace ServiceStack
{
public static class XLinqExtensions
{
public static string GetString(this XElement el, string name)
{
return el == null ? null : GetElementValueOrDefault(el, name, x => x.Value);
}
public static string GetStringAttributeOrDefault(this XElement element, string name)
{
var attr = AnyAttribute(element, name);
return attr == null ? null : GetAttributeValueOrDefault(attr, name, x => x.Value);
}
public static T GetAttributeValueOrDefault<T>(this XAttribute attr, string name, Func<XAttribute, T> converter)
{
if (converter == null)
{
throw new ArgumentNullException("converter");
}
return attr == null || string.IsNullOrEmpty(attr.Value) ? default(T) : converter(attr);
}
public static bool GetBool(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (bool)GetElement(el, name);
}
public static bool GetBoolOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (bool)x);
}
public static bool? GetNullableBool(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (bool?)childEl;
}
public static int GetInt(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (int)GetElement(el, name);
}
public static int GetIntOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (int)x);
}
public static int? GetNullableInt(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (int?)childEl;
}
public static long GetLong(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (long)GetElement(el, name);
}
public static long GetLongOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (long)x);
}
public static long? GetNullableLong(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (long?)childEl;
}
public static decimal GetDecimal(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (decimal)GetElement(el, name);
}
public static decimal GetDecimalOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (decimal)x);
}
public static decimal? GetNullableDecimal(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (decimal?)childEl;
}
public static DateTime GetDateTime(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (DateTime)GetElement(el, name);
}
public static DateTime GetDateTimeOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (DateTime)x);
}
public static DateTime? GetNullableDateTime(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (DateTime?)childEl;
}
public static TimeSpan GetTimeSpan(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (TimeSpan)GetElement(el, name);
}
public static TimeSpan GetTimeSpanOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (TimeSpan)x);
}
public static TimeSpan? GetNullableTimeSpan(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (TimeSpan?)childEl;
}
public static Guid GetGuid(this XElement el, string name)
{
AssertElementHasValue(el, name);
return (Guid)GetElement(el, name);
}
public static Guid GetGuidOrDefault(this XElement el, string name)
{
return GetElementValueOrDefault(el, name, x => (Guid)x);
}
public static Guid? GetNullableGuid(this XElement el, string name)
{
var childEl = GetElement(el, name);
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (Guid?)childEl;
}
public static T GetElementValueOrDefault<T>(this XElement element, string name, Func<XElement, T> converter)
{
if (converter == null)
{
throw new ArgumentNullException("converter");
}
var el = GetElement(element, name);
return el == null || string.IsNullOrEmpty(el.Value) ? default(T) : converter(el);
}
public static XElement GetElement(this XElement element, string name)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (name == null)
{
throw new ArgumentNullException("name");
}
return element.AnyElement(name);
}
public static void AssertElementHasValue(this XElement element, string name)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (name == null)
{
throw new ArgumentNullException("name");
}
var childEl = element.AnyElement(name);
if (childEl == null || string.IsNullOrEmpty(childEl.Value))
{
throw new ArgumentNullException(name, string.Format("{0} is required", name));
}
}
public static List<string> GetValues(this IEnumerable<XElement> els)
{
var values = new List<string>();
foreach (var el in els)
{
values.Add(el.Value);
}
return values;
}
public static XAttribute AnyAttribute(this XElement element, string name)
{
if (element == null) return null;
foreach (var attribute in element.Attributes())
{
if (attribute.Name.LocalName == name)
{
return attribute;
}
}
return null;
}
public static IEnumerable<XElement> AllElements(this XElement element, string name)
{
var els = new List<XElement>();
if (element == null) return els;
foreach (var node in element.Nodes())
{
if (node.NodeType != XmlNodeType.Element) continue;
var childEl = (XElement)node;
if (childEl.Name.LocalName == name)
{
els.Add(childEl);
}
}
return els;
}
public static XElement AnyElement(this XElement element, string name)
{
if (element == null) return null;
foreach (var node in element.Nodes())
{
if (node.NodeType != XmlNodeType.Element) continue;
var childEl = (XElement)node;
if (childEl.Name.LocalName == name)
{
return childEl;
}
}
return null;
}
public static XElement AnyElement(this IEnumerable<XElement> elements, string name)
{
foreach (var element in elements)
{
if (element.Name.LocalName == name)
{
return element;
}
}
return null;
}
public static IEnumerable<XElement> AllElements(this IEnumerable<XElement> elements, string name)
{
var els = new List<XElement>();
foreach (var element in elements)
{
els.AddRange(AllElements(element, name));
}
return els;
}
public static XElement FirstElement(this XElement element)
{
if (element.FirstNode.NodeType == XmlNodeType.Element)
{
return (XElement)element.FirstNode;
}
return null;
}
}
}
#endif