Archive

Posts Tagged ‘C#’

C# XML to Expando

October 9th, 2011 No comments

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: , , , ,

Deserializing XML twitter feeds.

July 2nd, 2010 No comments

1. Use xsd.exe to extract an xsd file from an xml feed.
Download a private xml feed for example from “http://api.twitter.com/1/statuses/user_timeline.xml” and store it to local disk. Use xsd.exe to generate the xsd.
2. Generate the classes from the xsd file you just generated.

xsd.exe yourxsd.xsd /c

3. Add the generated classes to your C# project.
4. Time for some fetching here is a short example:

// encode the username/password
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1/statuses/user_timeline.xml");
// set the method to GET
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
 
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
	XmlSerializer serializer = new XmlSerializer(typeof(statuses));
	statuses s = serializer.Deserialize(response.GetResponseStream()) as statuses;
 
	listBox1.Items.Clear();
	foreach (statusesStatus status in s.status)
	{
		listBox1.Items.Add(status.text);
	}
}
Categories: C# Tags: , , , , , , ,