Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A C# helper class to read and write XML from and to objects

View XmlHelper.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 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 289 290 291
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Linq;
 
namespace Nwazet.Commerce.Helpers {
public static class XmlHelper {
/// <summary>
/// Like Add, but chainable.
/// </summary>
/// <param name="el">The parent element.</param>
/// <param name="children">The elements to add.</param>
/// <returns>Itself</returns>
public static XElement AddEl(this XElement el, params XElement[] children) {
el.Add(children.Cast<object>());
return el;
}
 
/// <summary>
/// Gets the string value of an attribute, and null if the attribute doesn't exist.
/// </summary>
/// <param name="el">The element.</param>
/// <param name="name">The name of the attribute.</param>
/// <returns>The string value of the attribute if it exists, null otherwise.</returns>
public static string Attr(this XElement el, string name) {
var attr = el.Attribute(name);
return attr == null ? null : attr.Value;
}
 
/// <summary>
/// Gets a typed value from an attribute.
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="el">The element.</param>
/// <param name="name">The name of the attribute.</param>
/// <returns>The attribute value</returns>
public static T Attr<T>(this XElement el, string name) {
 
var attr = el.Attribute(name);
var type = typeof (T);
 
if (attr == null ||
((!type.IsValueType || Nullable.GetUnderlyingType(type) != null) && type != typeof(string) &&
"null".Equals(attr.Value, StringComparison.Ordinal))) {
 
return default(T);
}
 
if (type == typeof (string)) {
return (T)(object)attr.Value;
}
if (type == typeof (int)) {
return (T)(object)(int) attr; // Pretty, eh? OK, if you're so smart, find a better way. I'm waiting. Seriously, I'd love to not do this.
}
if (type == typeof (bool)) {
return (T)(object)(bool)attr;
}
if (type == typeof (DateTime)) {
return (T)(object)(DateTime)attr;
}
if (type == typeof (double)) {
return (T)(object)(double)attr;
}
if (type == typeof (float)) {
return (T)(object)(float)attr;
}
if (type == typeof (decimal)) {
return (T)(object)(decimal)attr;
}
if (type == typeof (int?)) {
return (T)(object)(int?)attr;
}
if (type == typeof (bool?)) {
return (T)(object)(bool?)attr;
}
if (type == typeof (DateTime?)) {
return (T)(object)(DateTime?)attr;
}
if (type == typeof (double?)) {
return (T)(object)(double?)attr;
}
if (type == typeof (float?)) {
return (T)(object)(float?)attr;
}
if (type == typeof (decimal?)) {
return (T)(object)(decimal?)attr;
}
throw new InvalidCastException(String.Format("Couldn't find how to deserialize to type {0}.", type.Name));
}
 
/// <summary>
/// Sets an attribute value. This is chainable.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="el">The element.</param>
/// <param name="name">The attribute name.</param>
/// <param name="value">The value to set.</param>
/// <returns>Itself</returns>
public static XElement Attr<T>(this XElement el, string name, T value) {
el.SetAttributeValue(name, value);
return el;
}
 
/// <summary>
/// Returns the text contents of a child element.
/// </summary>
/// <param name="el">The parent element.</param>
/// <param name="name">The name of the child element.</param>
/// <returns>The text for the child element, and null if it doesn't exist.</returns>
public static string El(this XElement el, string name) {
var childElement = el.Element(name);
return childElement == null ? null : childElement.Value;
}
 
/// <summary>
/// Creates and sets the value of a child element. This is chainable.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="el">The parent element.</param>
/// <param name="name">The name of the child element.</param>
/// <param name="value">The value to set.</param>
/// <returns>Itself</returns>
public static XElement El<T>(this XElement el, string name, T value) {
el.SetElementValue(name, value);
return el;
}
 
/// <summary>
/// Sets a property value from an attribute of the same name.
/// </summary>
/// <typeparam name="TTarget">The type of the target object.</typeparam>
/// <typeparam name="TProperty">The type of the target property</typeparam>
/// <param name="el">The element.</param>
/// <param name="target">The target object.</param>
/// <param name="targetExpression">The property expression.</param>
/// <returns>Itself</returns>
public static XElement FromAttr<TTarget, TProperty>(this XElement el, TTarget target,
Expression<Func<TTarget, TProperty>> targetExpression) {
 
var propertyInfo = GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var attr = el.Attribute(name);
 
if (attr == null) return el;
propertyInfo.SetValue(target, el.Attr<TProperty>(name), null);
return el;
}
 
/// <summary>
/// Sets an attribute with the value of a property of the same name.
/// </summary>
/// <typeparam name="TTarget">The type of the object.</typeparam>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="el">The element.</param>
/// <param name="target">The object.</param>
/// <param name="targetExpression">The property expression.</param>
/// <returns>Itself</returns>
public static XElement ToAttr<TTarget, TProperty>(this XElement el, TTarget target,
Expression<Func<TTarget, TProperty>> targetExpression) {
var propertyInfo = GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var val = propertyInfo.GetValue(target, null);
 
if (typeof (TProperty) == typeof (string)) {
el.Attr(name, (string) val);
}
else if (val == null) {
el.Attr(name, "null");
}
else if (typeof (TProperty) == typeof (int)) {
el.Attr(name, (int) val);
}
else if (typeof (TProperty) == typeof (bool)) {
el.Attr(name, (bool) val);
}
else if (typeof (TProperty) == typeof (DateTime)) {
el.Attr(name, (DateTime) val);
}
else if (typeof (TProperty) == typeof (double)) {
el.Attr(name, (double) val);
}
else if (typeof (TProperty) == typeof (float)) {
el.Attr(name, (float) val);
}
else if (typeof (TProperty) == typeof (decimal)) {
el.Attr(name, (decimal) val);
}
else if (typeof (TProperty) == typeof (int?)) {
el.Attr(name, (int?) val);
}
else if (typeof (TProperty) == typeof (bool?)) {
el.Attr(name, (bool?) val);
}
else if (typeof (TProperty) == typeof (DateTime?)) {
el.Attr(name, (DateTime?) val);
}
else if (typeof (TProperty) == typeof (double?)) {
el.Attr(name, (double?) val);
}
else if (typeof (TProperty) == typeof (float?)) {
el.Attr(name, (float?) val);
}
else if (typeof (TProperty) == typeof (decimal?)) {
el.Attr(name, (decimal?) val);
}
return el;
}
 
public static PropertyInfo GetPropertyInfo<TContext, TProperty>(Expression<Func<TContext, TProperty>> expression) {
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
throw new InvalidOperationException("Expression is not a member expression.");
var propertyInfo = memberExpression.Member as PropertyInfo;
if (propertyInfo == null) throw new InvalidOperationException("Expression is not for a property.");
return propertyInfo;
}
 
/// <summary>
/// Gives context to an XElement, enabling chained property operations.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
/// <param name="el">The element.</param>
/// <param name="context">The context.</param>
/// <returns>The element with context.</returns>
public static XElementWithContext<TContext> With<TContext>(this XElement el, TContext context) {
return new XElementWithContext<TContext>(el, context);
}
 
public class XElementWithContext<TContext> {
public XElementWithContext(XElement element, TContext context) {
Element = element;
Context = context;
}
 
public XElement Element { get; private set; }
public TContext Context { get; private set; }
 
public static implicit operator XElement(XElementWithContext<TContext> elementWithContext) {
return elementWithContext.Element;
}
 
/// <summary>
/// Replaces the current context with a new one, enabling chained action on different objects.
/// </summary>
/// <typeparam name="TNewContext">The type of the new context.</typeparam>
/// <param name="context">The new context.</param>
/// <returns>A new XElementWithContext, that has the new context.</returns>
public XElementWithContext<TNewContext> With<TNewContext>(TNewContext context) {
return new XElementWithContext<TNewContext>(Element, context);
}
 
/// <summary>
/// Sets the value of a context property as an attribute of the same name on the element.
/// </summary>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="targetExpression">The property expression.</param>
/// <returns>Itself</returns>
public XElementWithContext<TContext> ToAttr<TProperty>(
Expression<Func<TContext, TProperty>> targetExpression) {
Element.ToAttr(Context, targetExpression);
return this;
}
 
/// <summary>
/// Gets an attribute on the element and sets the property of the same name on the context with its value.
/// </summary>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="targetExpression">The property expression.</param>
/// <returns>Itself</returns>
public XElementWithContext<TContext> FromAttr<TProperty>(
Expression<Func<TContext, TProperty>> targetExpression) {
Element.FromAttr(Context, targetExpression);
return this;
}
 
/// <summary>
/// Evaluates an attribute from an expression.
/// It's a nice strongly-typed way to read attributes.
/// </summary>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <param name="expression">The property expression.</param>
/// <returns>The attribute, ready to be cast.</returns>
public TProperty Attr<TProperty>(Expression<Func<TContext, TProperty>> expression) {
var propertyInfo = GetPropertyInfo(expression);
var name = propertyInfo.Name;
return Element.Attr<TProperty>(name);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.