Created
July 5, 2011 13:11
-
-
Save asoftwareguy/1064812 to your computer and use it in GitHub Desktop.
Blog - XmlExpandoObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome concept!