/// <summary> /// Serialize the object as XML /// </summary> /// <param name="obj">Object to serialize</param> /// <returns>XML as string</returns> public string Serialize(object obj) { XDocument doc = new XDocument(); Type t = obj.GetType(); string name = t.Name; SerializeAsAttribute options = t.GetAttribute <SerializeAsAttribute>(); if (options != null) { name = options.TransformName(options.Name ?? name); } XElement root = new XElement(name.AsNamespaced(this.Namespace)); if (obj is IList) { string itemTypeName = ""; foreach (object item in (IList)obj) { Type type = item.GetType(); SerializeAsAttribute opts = type.GetAttribute <SerializeAsAttribute>(); if (opts != null) { itemTypeName = opts.TransformName(opts.Name ?? name); } if (itemTypeName == "") { itemTypeName = type.Name; } XElement instance = new XElement(itemTypeName.AsNamespaced(this.Namespace)); this.Map(instance, item); root.Add(instance); } } else { this.Map(root, obj); } if (this.RootElement.HasValue()) { XElement wrapper = new XElement(this.RootElement.AsNamespaced(this.Namespace), root); doc.Add(wrapper); } else { doc.Add(root); } return(doc.ToString()); }
private void Map(XContainer root, object obj) { Type objType = obj.GetType(); IEnumerable <PropertyInfo> props = from p in objType.GetProperties() let indexAttribute = p.GetAttribute <SerializeAsAttribute>() where p.CanRead && p.CanWrite orderby indexAttribute == null ? int.MaxValue : indexAttribute.Index select p; SerializeAsAttribute globalOptions = objType.GetAttribute <SerializeAsAttribute>(); foreach (PropertyInfo prop in props) { string name = prop.Name; object rawValue = prop.GetValue(obj, null); if (rawValue == null) { continue; } string value = this.GetSerializedValue(rawValue); Type propType = prop.PropertyType; bool useAttribute = false; SerializeAsAttribute settings = prop.GetAttribute <SerializeAsAttribute>(); if (settings != null) { name = settings.Name.HasValue() ? settings.Name : name; useAttribute = settings.Attribute; } SerializeAsAttribute options = prop.GetAttribute <SerializeAsAttribute>(); if (options != null) { name = options.TransformName(name); } else if (globalOptions != null) { name = globalOptions.TransformName(name); } XName nsName = name.AsNamespaced(this.Namespace); XElement element = new XElement(nsName); #if !WINDOWS_UWP if (propType.IsPrimitive || propType.IsValueType || propType == typeof(string)) #else if (propType.GetTypeInfo().IsPrimitive || propType.GetTypeInfo().IsValueType || propType == typeof(string)) #endif { if (useAttribute) { root.Add(new XAttribute(name, value)); continue; } element.Value = value; } else if (rawValue is IList) { string itemTypeName = ""; foreach (object item in (IList)rawValue) { if (itemTypeName == "") { Type type = item.GetType(); SerializeAsAttribute setting = type.GetAttribute <SerializeAsAttribute>(); itemTypeName = setting != null && setting.Name.HasValue() ? setting.Name : type.Name; } XElement instance = new XElement(itemTypeName.AsNamespaced(this.Namespace)); this.Map(instance, item); element.Add(instance); } } else { this.Map(element, rawValue); } root.Add(element); } }