/// <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 xdocument = new XDocument(); Type type1 = obj.GetType(); string name1 = type1.Name; SerializeAsAttribute attribute1 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>(type1); if (attribute1 != null) { name1 = attribute1.TransformName(attribute1.Name ?? name1); } XElement xelement1 = new XElement(XmlExtensions.AsNamespaced(name1, this.Namespace)); if (obj is IList) { string name2 = ""; foreach (object obj1 in (IEnumerable)obj) { Type type2 = obj1.GetType(); SerializeAsAttribute attribute2 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>(type2); if (attribute2 != null) { name2 = attribute2.TransformName(attribute2.Name ?? name1); } if (name2 == "") { name2 = type2.Name; } XElement xelement2 = new XElement(XmlExtensions.AsNamespaced(name2, this.Namespace)); this.Map((XContainer)xelement2, obj1); xelement1.Add((object)xelement2); } } else { this.Map((XContainer)xelement1, obj); } if (StringExtensions.HasValue(this.RootElement)) { XElement xelement2 = new XElement(XmlExtensions.AsNamespaced(this.RootElement, this.Namespace), (object)xelement1); xdocument.Add((object)xelement2); } else { xdocument.Add((object)xelement1); } return(xdocument.ToString()); }
private void Map(XContainer root, object obj) { Type type1 = obj.GetType(); IEnumerable <PropertyInfo> enumerable = Enumerable.Select(Enumerable.OrderBy(Enumerable.Where(Enumerable.Select((IEnumerable <PropertyInfo>)type1.GetProperties(), p => new { p = p, indexAttribute = ReflectionExtensions.GetAttribute <SerializeAsAttribute>((MemberInfo)p) }), param0 => { if (param0.p.CanRead) { return(param0.p.CanWrite); } return(false); }), param0 => { if (param0.indexAttribute != null) { return(param0.indexAttribute.Index); } return(int.MaxValue); }), param0 => param0.p); SerializeAsAttribute attribute1 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>(type1); foreach (PropertyInfo propertyInfo in enumerable) { string str = propertyInfo.Name; object obj1 = propertyInfo.GetValue(obj, (object[])null); if (obj1 != null) { string serializedValue = this.GetSerializedValue(obj1); Type propertyType = propertyInfo.PropertyType; bool flag = false; SerializeAsAttribute attribute2 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>((MemberInfo)propertyInfo); if (attribute2 != null) { str = StringExtensions.HasValue(attribute2.Name) ? attribute2.Name : str; flag = attribute2.Attribute; } SerializeAsAttribute attribute3 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>((MemberInfo)propertyInfo); if (attribute3 != null) { str = attribute3.TransformName(str); } else if (attribute1 != null) { str = attribute1.TransformName(str); } XElement xelement1 = new XElement(XmlExtensions.AsNamespaced(str, this.Namespace)); if (propertyType.IsPrimitive || propertyType.IsValueType || propertyType == typeof(string)) { if (flag) { root.Add((object)new XAttribute((XName)str, (object)serializedValue)); continue; } xelement1.Value = serializedValue; } else if (obj1 is IList) { string name = ""; foreach (object obj2 in (IEnumerable)obj1) { if (name == "") { Type type2 = obj2.GetType(); SerializeAsAttribute attribute4 = ReflectionExtensions.GetAttribute <SerializeAsAttribute>(type2); name = attribute4 == null || !StringExtensions.HasValue(attribute4.Name) ? type2.Name : attribute4.Name; } XElement xelement2 = new XElement(XmlExtensions.AsNamespaced(name, this.Namespace)); this.Map((XContainer)xelement2, obj2); xelement1.Add((object)xelement2); } } else { this.Map((XContainer)xelement1, obj1); } root.Add((object)xelement1); } } }
private void Map(XContainer root, object obj) { Type objType = obj.GetType(); var props = from p in objType.GetProperties() let indexAttribute = p.GetAttribute <SerializeAsAttribute>() where p.CanRead && p.CanWrite orderby indexAttribute?.Index ?? int.MaxValue select p; SerializeAsAttribute globalOptions = objType.GetAttribute <SerializeAsAttribute>(); bool textContentAttributeAlreadyUsed = false; foreach (var prop in props) { var name = prop.Name; var rawValue = prop.GetValue(obj, null); if (rawValue == null) { continue; } string value = this.GetSerializedValue(rawValue); Type propType = prop.PropertyType; bool useAttribute = false; bool setTextContent = false; SerializeAsAttribute options = prop.GetAttribute <SerializeAsAttribute>(); if (options != null) { name = options.Name.HasValue() ? options.Name : name; name = options.TransformName(name); useAttribute = options.Attribute; setTextContent = options.Content; if (textContentAttributeAlreadyUsed && setTextContent) { throw new ArgumentException("Class cannot have two properties marked with " + "SerializeAs(Content = true) attribute."); } textContentAttributeAlreadyUsed |= setTextContent; } else if (globalOptions != null) { name = globalOptions.TransformName(name); } var nsName = name.AsNamespaced(Namespace); var element = new XElement(nsName); if (propType.GetTypeInfo().IsPrimitive || propType.GetTypeInfo().IsValueType || propType == typeof(string)) { if (useAttribute) { root.Add(new XAttribute(name, value)); continue; } else if (setTextContent) { root.Add(new XText(value)); continue; } element.Value = value; } else if (rawValue is IList) { var itemTypeName = ""; foreach (var item in (IList)rawValue) { if (itemTypeName == "") { var type = item.GetType(); var setting = type.GetAttribute <SerializeAsAttribute>(); itemTypeName = setting != null && setting.Name.HasValue() ? setting.Name : type.Name; } var instance = new XElement(itemTypeName.AsNamespaced(Namespace)); Map(instance, item); element.Add(instance); } } else { Map(element, rawValue); } root.Add(element); } }