Fun with Anonymous Types and LINQ to XML

[Blog Map]  This blog is inactive.  New blog:

(August 2, 2008: This post uses the wrong approach.  I've written a new post that shows the way to accomplish the same thing in the functional style.) 

You can, of course, use C# anonymous types to create types that are more than simple tuples. For example, you can nest anonymous types to create an object graph:

var PurchaseOrder = new {
    PurchaseOrderNumber = "99503",
    OrderDate = DateTime.Parse("1999-10-20"),
    Addresses = new [] {
        new {
            AddressType = "Shipping",
            Name = "Alice Smith",
            Street = "123 Maple Street",
            City = "Mill Valley",
            State = "CA",
            Zip = "90952",
            Country = "USA"
        new {
            AddressType = "Billing",
            Name = "Robert Smith",
            Street = "8 Oak Avenue",
            City = "Old Town",
            State = "PA",
            Zip = "95819",
            Country = "USA",
    Comment = "Hurry, my lawn is going wild",
    Items = new [] {
        new {
            PartNumber = "872-AA",
            ProductName = "Lawnmower",
            Quantity = 1,
            USPrice = 148.95,
            Comment = "Confirm this is electric",
            ShipDate = DateTime.MinValue
        new {
            PartNumber = "926-AA",
            ProductName = "Baby Monitor",
            Quantity = 2,
            USPrice = 39.98,
            Comment = (string)null,
            ShipDate = DateTime.Parse("1999-05-21")

This also has applicability with LINQ to XML. For example, you can use a small method that uses reflection to populate an XML tree. The following looks for public properties in the type and iterates over them, creating XElement objects. If a property implements the IEnumerable interface, then the method uses recursion to iterate over the children of the property.

static void ObjectGraphToXElement(XElement parent, object o) {
    MemberInfo[] members =
        o.GetType().GetMembers(BindingFlags.Public |
    foreach (MemberInfo m in members) {
        PropertyInfo p = m as PropertyInfo;
        if (p != null) {
            Type t = p.PropertyType;
            object val = p.GetValue(o, null);
            if (val != null) {
                if (t.IsValueType || t == typeof(string))
                    parent.Add(new XElement(m.Name, val));
                else {
                    XElement newParent = new XElement(m.Name);
                    foreach (var v in (val as IEnumerable))
                        ObjectGraphToXElement(newParent, v);

The following code uses ObjectGraphToXElement to create an XML tree:

XElement po = new XElement("PurchaseOrder");
ObjectGraphToXElement(po, PurchaseOrder);

When run, it produces the following output:

    <Name>Alice Smith</Name>
    <Street>123 Maple Street</Street>
    <City>Mill Valley</City>
    <Name>Robert Smith</Name>
    <Street>8 Oak Avenue</Street>
    <City>Old Town</City>
  <Comment>Hurry, my lawn is going wild</Comment>
    <Comment>Confirm this is electric</Comment>
    <ProductName>Baby Monitor</ProductName>

Pretty cool, eh?

Comments (2)

  1. Just as soon as I learn something, I blog it. This has a potential pitfall – sometimes I blog something

Skip to main content