private static void Transform(XNode node, Type type)
{
var element = node as XElement;
if (element == null) return;
element.Attributes().Remove();
var t = type.GetTypeInfo();
//RWM: Do the recursion first, so matching elements in child objects don't accidentally get picked up early.
//TODO: Handle generic lists
foreach (var prop in t.DeclaredProperties.Where(c => !(c.PropertyType.IsSimpleType())))
{
Debug.WriteLine(prop.Name);
var xnode = element.Descendants().FirstOrDefault(c => c.Name.ToString() == prop.Name);
if (xnode != null)
{
Transform(xnode, prop.PropertyType);
}
}
foreach (var prop in t.DeclaredProperties.Where(c => c.GetCustomAttributes(typeof(XmlAttributeAttribute), true).Any()))
//foreach (var prop in type.GetProperties().Where(c => c.GetCustomAttributes(typeof(XmlAttributeAttribute), true).Any()))
{
var attribs = prop.GetCustomAttributes(true);
if (attribs.Any(c => c is IgnoreDataMemberAttribute || c is XmlIgnoreAttribute)) continue;
var xnode = element.Descendants().FirstOrDefault(c => c.Name.ToString() == prop.Name);
if (xnode == null) continue;
element.SetAttributeValue(xnode.Name, xnode.Value);
xnode.Remove();
}
//TODO: RWM: Handle time formats properly.
//foreach (var prop in t.DeclaredProperties.Where(c => c.PropertyType == typeof (DateTime)))
//{
// var xnode = element.Descendants().FirstOrDefault(c => c.Name.ToString() == prop.Name);
// if (xnode == null) continue;
// var newValue = DateTime.ParseExact(element.Value, DateFormat, null);
// element.Value = XmlConvert.ToString(newValue);
//}
}