convert an XML document into a C# dynamic object.

http://www.itdevspace.com/2012/07/parse-xml-to-dynamic-object-in-c.html

This guy has a great class to convert an XML document into a C# dynamic object. very useful

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Dynamic;

namespace LinkedInAggregator.Core
{
/// <summary>
/// Converts XML data into a C# Dynamic Object
/// http://www.itdevspace.com/2012/07/parse-xml-to-dynamic-object-in-c.html
/// </summary>
public class XmlToDynamic
{
public static void Parse(dynamic parent, XElement node)
{
if (node.HasElements)
{
if (node.Elements(node.Elements().First().Name.LocalName).Count() > 1)
{
//list
var item = new ExpandoObject();
var list = new List<dynamic>();
foreach (var element in node.Elements())
{
Parse(list, element);
}

AddProperty(item, node.Elements().First().Name.LocalName, list);
AddProperty(parent, node.Name.ToString(), item);
}
else
{
var item = new ExpandoObject();

foreach (var attribute in node.Attributes())
{
AddProperty(item, attribute.Name.ToString(), attribute.Value.Trim());
}

//element
foreach (var element in node.Elements())
{
Parse(item, element);
}

AddProperty(parent, node.Name.ToString(), item);
}
}
else
{
AddProperty(parent, node.Name.ToString(), node.Value.Trim());
}
}

private static void AddProperty(dynamic parent, string name, object value)
{
if (parent is List<dynamic>)
{
(parent as List<dynamic>).Add(value);
}
else
{
(parent as IDictionary<String, object>)[name] = value;
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *