Skip to content

Instantly share code, notes, and snippets.

@asoftwareguy
Created July 5, 2011 13:11
Show Gist options
  • Save asoftwareguy/1064812 to your computer and use it in GitHub Desktop.
Save asoftwareguy/1064812 to your computer and use it in GitHub Desktop.
Blog - XmlExpandoObject
using System;
using System.Dynamic;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
namespace Test
{
public class XmlExpandoObject
{
private dynamic _fullXmlExpando;
public XmlExpandoObject(String xmlFile, string componentName, String configName)
{
var doc = XDocument.Load(xmlFile);
var nodes = from node in doc.Root.Descendants(componentName)
select node.Element(configName);
this._fullXmlExpando = GetExpandoForNodes(nodes);
}
private dynamic GetExpandoForNodes(IEnumerable<XElement> nodes)
{
dynamic config = new ExpandoObject();
foreach (var n in nodes)
{
if (n.Descendants().Count() == 0)
{
(config as IDictionary<String, object>)[n.Name.ToString()] = n.Value.Trim();
}
else
{
dynamic child = GetExpandoForNodes(n.Descendants());
(config as IDictionary<String, object>)[n.Name.ToString()] = child;
}
}
return config;
}
public dynamic GetXmlExpando()
{
return _fullXmlExpando;
}
}
}
@tonyo1
Copy link

tonyo1 commented May 8, 2014

Awesome concept!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment