Skip to content

Instantly share code, notes, and snippets.

@msarchet
Created April 23, 2011 18:41
Show Gist options
  • Save msarchet/938860 to your computer and use it in GitHub Desktop.
Save msarchet/938860 to your computer and use it in GitHub Desktop.
XLinq Extensions
using System;
using System.Linq;
using System.Xml.Linq;
using System.Runtime.CompilerServices;
using System.Diagnostics;
//This class is simply used as a demonsration on how to use the extensions
public class Foo
{
public static void Main()
{
XElement initial = new XElement("test", new XElement("Child"), new XElement("Child1", new XElement("Child1a")));
for(int i = 0; i < 10; i++)
{
XElement merge = new XElement("test", new XElement("Child" + i));
initial.Merge(merge);
}
Console.WriteLine(initial.ToString());
Console.WriteLine(new XElement("test", "Value").ValueOrEmpty());
Console.WriteLine(new XElement("test", "1").ValueOrEmpty());
Console.WriteLine(new XElement("test", "1").ValueOrDefault<int>());
Console.WriteLine(new XElement("test", "1.000005").ValueOrDefault<double>());
Console.WriteLine(new XElement("test", new XElement("test1", "True")).ValueOrDefault<Boolean>());
Console.WriteLine(new XElement("test", "1").ValueOrDefault<test>());
Console.WriteLine(new XElement("test").ValueOrDefault<int>());
Console.WriteLine(new XElement("test").ValueOrDefault<double>());
Console.WriteLine(new XElement("test").ValueOrDefault<String>());
Console.WriteLine(new XElement("test", new XElement("test1")).ValueOrDefault<Boolean>());
Console.WriteLine(new XElement("test").ValueOrDefault<test>());
Console.Read();
}
}
//Used for example purposes
public enum test
{
one,
two
}
//Extensions class
public static class XLinqExtensions
{
//Merges Together Multiple similar XElements
//In the future planning on having this have Join style behavior
//And the ability to merge from certain levels
//And the ability to compare on a certain node
public static XElement Merge(this XElement obj, XElement merges)
{
XElement objCopy = obj;
if (objCopy.Name == merges.Name)
{
foreach(XElement descendant in merges.Elements())
{
if(objCopy.Elements().Where(x =>x.Name == descendant.Name).Any())
{
XElement temp = objCopy.Element(descendant.Name);
//temp = new XElement(objCopy.Element(descendant.Name).Merge(descendant));
temp = (objCopy.Element(descendant.Name).Merge(descendant));
}
else
{
objCopy.Add(descendant);
}
}
}
else
{
objCopy.Add(merges);
}
return objCopy;
}
//Returns the Value at the node or String.Empty
public static String ValueOrEmpty(this XElement element)
{
return element.Value ?? "";
}
//Returns a Value of Type T or a default T
public static T ValueOrDefault<T>(this XElement element)
{
return (T)genConvert<T>(element.Value);
}
//Used for conversion of the Value to a T
private static T genConvert<T>(String str)
{
//Try to get a converter for T
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
//Is there a converter
if(converter != null)
{
//For now try the conversion, if it doesn't work return the default value for T
try
{
return (T)converter.ConvertFromString(str);
}
catch
{
return default(T);
}
}
return default(T);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment