Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@tonyo1 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