using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using System.Xml.Linq;
namespace TiaanDotCom.Samples.Reflection.RouteValueDictionaryDemo
{ class Program
{ static XElement GetObjectAsXml(
string rootElementName, object simpleDataObject)
{ // Extract property values of object (reflection indirectly)
IDictionary<string, object> keyValueSet =
new RouteValueDictionary(simpleDataObject);
// Convert key-value set into XML
return new XElement(
rootElementName ?? simpleDataObject.GetType().Name,
keyValueSet.Select(@item =>
new XElement(@item.Key, @item.Value)));
}
static void Main(string[] args)
{ // Illustrate concepts through some examples
Console.WriteLine("<!-- Example 1: Well-defined type -->"); Console.WriteLine(GetObjectAsXml(null,
DateTime.Parse("2009-03-16"))); Console.WriteLine();
Console.WriteLine("<!-- Example 2: Anonymous type -->"); Console.WriteLine(GetObjectAsXml("root", new { x = 123,
y = "Hello world",
z = new XElement[] { new XElement("a", null), new XElement("b", DateTime.MaxValue) } })); }
}
}