Home > C# > C# XML to Expando

C# XML to Expando

Made up this extension method for XDocuments to be able to return an Expando object on which the properties of the XML can be navigated. This is still a little bit rough. I found out there is a ElasticObject that probably works a lot better. But this is just a little ‘proof of concept’.
Note that attributes in the XML are not bound in the Expando object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Xml.Linq;
 
namespace Famvdploeg.Com
{
    public static class XDocumentExtensions
    {
        public static dynamic ToExpandoObject(this XDocument document)
        {
            return ParseNode(document.Root);
        }
 
        private static dynamic ParseNode(XElement item)
        {
            dynamic expando = new ExpandoObject();
            var props = expando as IDictionary<string, object>;
 
            /* Properties of current node. Single and Lists are created here dynamic */
            IEnumerable<IGrouping<string, XElement>> groupedProperties = item.Elements().Where(element => !element.HasElements).GroupBy(x => x.Name.ToString());
 
            foreach (IGrouping<string, XElement> propertyGroup in groupedProperties)
            {
                int nrElements = propertyGroup.Count();
                if (nrElements == 1)
                {
                    props[propertyGroup.Key.ToValidPropertyName()] = propertyGroup.First().Value;
                }
                else
                {
                    var multipleValues = new List<string>();
 
                    foreach (XElement element in propertyGroup)
                    {
                        multipleValues.Add(element.Value);
                    }
 
                    props[propertyGroup.Key.ToValidPropertyName()] = multipleValues;
                }
            }
 
            /* Children of current node. Single and Lists are created here dynamic */
            IEnumerable<IGrouping<string, XElement>> groupedElements = item.Elements().Where(element => element.HasElements).GroupBy(x => x.Name.ToString());
 
            foreach (IGrouping<string, XElement> elementGroup in groupedElements)
            {
                int nrElements = elementGroup.Count();
                if (nrElements == 1)
                {
                    props[elementGroup.Key.ToValidPropertyName()] = ParseNode(elementGroup.First());
                }
                else
                {
                    var multipleElements = new List<dynamic>();
 
                    foreach (XElement element in elementGroup)
                    {
                        multipleElements.Add(ParseNode(element));
                    }
 
                    props[elementGroup.Key.ToValidPropertyName()] = multipleElements;
                }
            }
 
            return expando;
        }
    }
}

Categories: C# Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Time limit is exhausted. Please reload CAPTCHA.