1
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
3
// ServiceStack: Useful extensions to simplify parsing xml with XLinq
6
// Demis Bellot (demis.bellot@gmail.com)
8
// Copyright 2012 ServiceStack
10
// Licensed under the new BSD license.
14
using System.Collections.Generic;
16
using System.Xml.Linq;
18
namespace ServiceStack.ServiceModel
20
public static class XLinqExtensions
22
public static string GetString(this XElement el, string name)
24
return el == null ? null : GetElementValueOrDefault(el, name, x => x.Value);
27
public static string GetStringAttributeOrDefault(this XElement element, string name)
29
var attr = AnyAttribute(element, name);
30
return attr == null ? null : GetAttributeValueOrDefault(attr, name, x => x.Value);
33
public static bool GetBool(this XElement el, string name)
35
AssertElementHasValue(el, name);
36
return (bool)GetElement(el, name);
39
public static bool GetBoolOrDefault(this XElement el, string name)
41
return GetElementValueOrDefault(el, name, x => (bool)x);
44
public static bool? GetNullableBool(this XElement el, string name)
46
var childEl = GetElement(el, name);
47
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (bool?)childEl;
50
public static int GetInt(this XElement el, string name)
52
AssertElementHasValue(el, name);
53
return (int)GetElement(el, name);
56
public static int GetIntOrDefault(this XElement el, string name)
58
return GetElementValueOrDefault(el, name, x => (int)x);
61
public static int? GetNullableInt(this XElement el, string name)
63
var childEl = GetElement(el, name);
64
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (int?)childEl;
67
public static long GetLong(this XElement el, string name)
69
AssertElementHasValue(el, name);
70
return (long)GetElement(el, name);
73
public static long GetLongOrDefault(this XElement el, string name)
75
return GetElementValueOrDefault(el, name, x => (long)x);
78
public static long? GetNullableLong(this XElement el, string name)
80
var childEl = GetElement(el, name);
81
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (long?)childEl;
84
public static decimal GetDecimal(this XElement el, string name)
86
AssertElementHasValue(el, name);
87
return (decimal)GetElement(el, name);
90
public static decimal GetDecimalOrDefault(this XElement el, string name)
92
return GetElementValueOrDefault(el, name, x => (decimal)x);
95
public static decimal? GetNullableDecimal(this XElement el, string name)
97
var childEl = GetElement(el, name);
98
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (decimal?)childEl;
101
public static DateTime GetDateTime(this XElement el, string name)
103
AssertElementHasValue(el, name);
104
return (DateTime)GetElement(el, name);
107
public static DateTime GetDateTimeOrDefault(this XElement el, string name)
109
return GetElementValueOrDefault(el, name, x => (DateTime)x);
112
public static DateTime? GetNullableDateTime(this XElement el, string name)
114
var childEl = GetElement(el, name);
115
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (DateTime?)childEl;
118
public static TimeSpan GetTimeSpan(this XElement el, string name)
120
AssertElementHasValue(el, name);
121
return (TimeSpan)GetElement(el, name);
124
public static TimeSpan GetTimeSpanOrDefault(this XElement el, string name)
126
return GetElementValueOrDefault(el, name, x => (TimeSpan)x);
129
public static TimeSpan? GetNullableTimeSpan(this XElement el, string name)
131
var childEl = GetElement(el, name);
132
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (TimeSpan?)childEl;
135
public static Guid GetGuid(this XElement el, string name)
137
AssertElementHasValue(el, name);
138
return (Guid)GetElement(el, name);
141
public static Guid GetGuidOrDefault(this XElement el, string name)
143
return GetElementValueOrDefault(el, name, x => (Guid)x);
146
public static Guid? GetNullableGuid(this XElement el, string name)
148
var childEl = GetElement(el, name);
149
return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (Guid?)childEl;
152
public static T GetElementValueOrDefault<T>(this XElement element, string name, Func<XElement, T> converter)
154
if (converter == null)
156
throw new ArgumentNullException("converter");
158
var el = GetElement(element, name);
159
return el == null || string.IsNullOrEmpty(el.Value) ? default(T) : converter(el);
162
public static XElement GetElement(this XElement element, string name)
166
throw new ArgumentNullException("element");
170
throw new ArgumentNullException("name");
172
return element.AnyElement(name);
175
public static T GetAttributeValueOrDefault<T>(this XAttribute attr, string name, Func<XAttribute, T> converter)
177
if (converter == null)
179
throw new ArgumentNullException("converter");
181
return attr == null || string.IsNullOrEmpty(attr.Value) ? default(T) : converter(attr);
184
public static void AssertExactlyOneResult(this XElement queryListItems, string referenceNumber, string formType)
186
int count = Convert.ToInt32(queryListItems.AnyAttribute("ItemCount").Value);
188
throw new InvalidOperationException(string.Format("There is no {0} for with a deal reference number {1}", formType, referenceNumber));
190
throw new InvalidOperationException(
191
string.Format("There are more than one {0}s with deal reference number {1}", formType, referenceNumber));
194
public static void AssertElementHasValue(this XElement element, string name)
198
throw new ArgumentNullException("element");
202
throw new ArgumentNullException("name");
204
var childEl = element.AnyElement(name);
205
if (childEl == null || string.IsNullOrEmpty(childEl.Value))
207
throw new ArgumentNullException(name, string.Format("{0} is required", name));
211
public static List<string> GetValues(this IEnumerable<XElement> els)
213
var values = new List<string>();
214
foreach (var el in els)
216
values.Add(el.Value);
221
public static XAttribute AnyAttribute(this XElement element, string name)
223
if (element == null) return null;
224
foreach (var attribute in element.Attributes())
226
if (attribute.Name.LocalName == name)
234
public static IEnumerable<XElement> AllElements(this XElement element, string name)
236
var els = new List<XElement>();
237
if (element == null) return els;
238
foreach (var node in element.Nodes())
240
if (node.NodeType != XmlNodeType.Element) continue;
241
var childEl = (XElement)node;
242
if (childEl.Name.LocalName == name)
250
public static XElement AnyElement(this XElement element, string name)
252
if (element == null) return null;
253
foreach (var node in element.Nodes())
255
if (node.NodeType != XmlNodeType.Element) continue;
256
var childEl = (XElement)node;
257
if (childEl.Name.LocalName == name)
265
public static XElement AnyElement(this IEnumerable<XElement> elements, string name)
267
foreach (var element in elements)
269
if (element.Name.LocalName == name)
277
public static IEnumerable<XElement> AllElements(this IEnumerable<XElement> elements, string name)
279
var els = new List<XElement>();
280
foreach (var element in elements)
282
els.AddRange(AllElements(element, name));
287
public static XElement FirstElement(this XElement element)
289
if (element.FirstNode.NodeType == XmlNodeType.Element)
291
return (XElement)element.FirstNode;