1 #if !SILVERLIGHT && !MONOTOUCH && !XBOX 2 // 3 // ServiceStack: Useful extensions to simplify parsing xml with XLinq 4 // 5 // Authors: 6 // Demis Bellot (demis.bellot@gmail.com) 7 // 8 // Copyright 2010 Liquidbit Ltd. 9 // 10 // Licensed under the same terms of reddis and ServiceStack: new BSD license. 11 // 12 13 using System; 14 using System.Collections.Generic; 15 using System.Xml; 16 using System.Xml.Linq; 17 18 namespace ServiceStack.ServiceModel.Extensions 19 { 20 public static class XLinqExtensions 21 { GetString(this XElement el, string name)22 public static string GetString(this XElement el, string name) 23 { 24 return el == null ? null : GetElementValueOrDefault(el, name, x => x.Value); 25 } 26 GetBool(this XElement el, string name)27 public static bool GetBool(this XElement el, string name) 28 { 29 AssertElementHasValue(el, name); 30 return (bool)GetElement(el, name); 31 } 32 GetBoolOrDefault(this XElement el, string name)33 public static bool GetBoolOrDefault(this XElement el, string name) 34 { 35 return GetElementValueOrDefault(el, name, x => (bool)x); 36 } 37 GetNullableBool(this XElement el, string name)38 public static bool? GetNullableBool(this XElement el, string name) 39 { 40 var childEl = GetElement(el, name); 41 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (bool?)childEl; 42 } 43 GetInt(this XElement el, string name)44 public static int GetInt(this XElement el, string name) 45 { 46 AssertElementHasValue(el, name); 47 return (int)GetElement(el, name); 48 } 49 GetIntOrDefault(this XElement el, string name)50 public static int GetIntOrDefault(this XElement el, string name) 51 { 52 return GetElementValueOrDefault(el, name, x => (int) x); 53 } 54 GetNullableInt(this XElement el, string name)55 public static int? GetNullableInt(this XElement el, string name) 56 { 57 var childEl = GetElement(el, name); 58 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (int?) childEl; 59 } 60 GetLong(this XElement el, string name)61 public static long GetLong(this XElement el, string name) 62 { 63 AssertElementHasValue(el, name); 64 return (long)GetElement(el, name); 65 } 66 GetLongOrDefault(this XElement el, string name)67 public static long GetLongOrDefault(this XElement el, string name) 68 { 69 return GetElementValueOrDefault(el, name, x => (long)x); 70 } 71 GetNullableLong(this XElement el, string name)72 public static long? GetNullableLong(this XElement el, string name) 73 { 74 var childEl = GetElement(el, name); 75 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (long?)childEl; 76 } 77 GetDecimal(this XElement el, string name)78 public static decimal GetDecimal(this XElement el, string name) 79 { 80 AssertElementHasValue(el, name); 81 return (decimal)GetElement(el, name); 82 } 83 GetDecimalOrDefault(this XElement el, string name)84 public static decimal GetDecimalOrDefault(this XElement el, string name) 85 { 86 return GetElementValueOrDefault(el, name, x => (decimal)x); 87 } 88 GetNullableDecimal(this XElement el, string name)89 public static decimal? GetNullableDecimal(this XElement el, string name) 90 { 91 var childEl = GetElement(el, name); 92 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (decimal?)childEl; 93 } 94 GetDateTime(this XElement el, string name)95 public static DateTime GetDateTime(this XElement el, string name) 96 { 97 AssertElementHasValue(el, name); 98 return (DateTime)GetElement(el, name); 99 } 100 GetDateTimeOrDefault(this XElement el, string name)101 public static DateTime GetDateTimeOrDefault(this XElement el, string name) 102 { 103 return GetElementValueOrDefault(el, name, x => (DateTime)x); 104 } 105 GetNullableDateTime(this XElement el, string name)106 public static DateTime? GetNullableDateTime(this XElement el, string name) 107 { 108 var childEl = GetElement(el, name); 109 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (DateTime?)childEl; 110 } 111 GetTimeSpan(this XElement el, string name)112 public static TimeSpan GetTimeSpan(this XElement el, string name) 113 { 114 AssertElementHasValue(el, name); 115 return (TimeSpan)GetElement(el, name); 116 } 117 GetTimeSpanOrDefault(this XElement el, string name)118 public static TimeSpan GetTimeSpanOrDefault(this XElement el, string name) 119 { 120 return GetElementValueOrDefault(el, name, x => (TimeSpan)x); 121 } 122 GetNullableTimeSpan(this XElement el, string name)123 public static TimeSpan? GetNullableTimeSpan(this XElement el, string name) 124 { 125 var childEl = GetElement(el, name); 126 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (TimeSpan?)childEl; 127 } 128 GetGuid(this XElement el, string name)129 public static Guid GetGuid(this XElement el, string name) 130 { 131 AssertElementHasValue(el, name); 132 return (Guid)GetElement(el, name); 133 } 134 GetGuidOrDefault(this XElement el, string name)135 public static Guid GetGuidOrDefault(this XElement el, string name) 136 { 137 return GetElementValueOrDefault(el, name, x => (Guid)x); 138 } 139 GetNullableGuid(this XElement el, string name)140 public static Guid? GetNullableGuid(this XElement el, string name) 141 { 142 var childEl = GetElement(el, name); 143 return childEl == null || string.IsNullOrEmpty(childEl.Value) ? null : (Guid?)childEl; 144 } 145 GetElementValueOrDefault(this XElement element, string name, Func<XElement, T> converter)146 public static T GetElementValueOrDefault<T>(this XElement element, string name, Func<XElement, T> converter) 147 { 148 if (converter == null) 149 { 150 throw new ArgumentNullException("converter"); 151 } 152 var el = GetElement(element, name); 153 return el == null || string.IsNullOrEmpty(el.Value) ? default(T) : converter(el); 154 } 155 GetElement(this XElement element, string name)156 public static XElement GetElement(this XElement element, string name) 157 { 158 if (element == null) 159 { 160 throw new ArgumentNullException("element"); 161 } 162 if (name == null) 163 { 164 throw new ArgumentNullException("name"); 165 } 166 return element.AnyElement(name); 167 } 168 AssertElementHasValue(this XElement element, string name)169 public static void AssertElementHasValue(this XElement element, string name) 170 { 171 if (element == null) 172 { 173 throw new ArgumentNullException("element"); 174 } 175 if (name == null) 176 { 177 throw new ArgumentNullException("name"); 178 } 179 var childEl = element.AnyElement(name); 180 if (childEl == null || string.IsNullOrEmpty(childEl.Value)) 181 { 182 throw new ArgumentNullException(name, string.Format("{0} is required", name)); 183 } 184 } 185 GetValues(this IEnumerable<XElement> els)186 public static List<string> GetValues(this IEnumerable<XElement> els) 187 { 188 var values = new List<string>(); 189 foreach (var el in els) 190 { 191 values.Add(el.Value); 192 } 193 return values; 194 } 195 AnyAttribute(this XElement element, string name)196 public static XAttribute AnyAttribute(this XElement element, string name) 197 { 198 if (element == null) return null; 199 foreach (var attribute in element.Attributes()) 200 { 201 if (attribute.Name.LocalName == name) 202 { 203 return attribute; 204 } 205 } 206 return null; 207 } 208 AllElements(this XElement element, string name)209 public static IEnumerable<XElement> AllElements(this XElement element, string name) 210 { 211 var els = new List<XElement>(); 212 if (element == null) return els; 213 foreach (var node in element.Nodes()) 214 { 215 if (node.NodeType != XmlNodeType.Element) continue; 216 var childEl = (XElement)node; 217 if (childEl.Name.LocalName == name) 218 { 219 els.Add(childEl); 220 } 221 } 222 return els; 223 } 224 AnyElement(this XElement element, string name)225 public static XElement AnyElement(this XElement element, string name) 226 { 227 if (element == null) return null; 228 foreach (var node in element.Nodes()) 229 { 230 if (node.NodeType != XmlNodeType.Element) continue; 231 var childEl = (XElement)node; 232 if (childEl.Name.LocalName == name) 233 { 234 return childEl; 235 } 236 } 237 return null; 238 } 239 AnyElement(this IEnumerable<XElement> elements, string name)240 public static XElement AnyElement(this IEnumerable<XElement> elements, string name) 241 { 242 foreach (var element in elements) 243 { 244 if (element.Name.LocalName == name) 245 { 246 return element; 247 } 248 } 249 return null; 250 } 251 AllElements(this IEnumerable<XElement> elements, string name)252 public static IEnumerable<XElement> AllElements(this IEnumerable<XElement> elements, string name) 253 { 254 var els = new List<XElement>(); 255 foreach (var element in elements) 256 { 257 els.AddRange(AllElements(element, name)); 258 } 259 return els; 260 } 261 FirstElement(this XElement element)262 public static XElement FirstElement(this XElement element) 263 { 264 if (element.FirstNode.NodeType == XmlNodeType.Element) 265 { 266 return (XElement) element.FirstNode; 267 } 268 return null; 269 } 270 271 } 272 } 273 #endif 274