protected virtual XAttribute GetAttributeByName(XElement root, XName name) { List <XName> names = new List <XName>() { (XName)name.LocalName, XmlExtensions.AsNamespaced(name.LocalName.ToLower(), name.NamespaceName), XmlExtensions.AsNamespaced(StringExtensions.ToCamelCase(name.LocalName, this.Culture), name.NamespaceName) }; return(Enumerable.FirstOrDefault <XAttribute>(Extensions.Attributes((IEnumerable <XElement>)Enumerable.OrderBy <XElement, int>(root.DescendantsAndSelf(), (Func <XElement, int>)(d => Enumerable.Count <XElement>(d.Ancestors())))), (Func <XAttribute, bool>)(d => names.Contains((XName)StringExtensions.RemoveUnderscoresAndDashes(d.Name.LocalName))))); }
/// <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()); }
public virtual T Deserialize <T>(IRestResponse response) { if (string.IsNullOrEmpty(response.Content)) { return(default(T)); } XDocument xdoc = XDocument.Parse(response.Content); XElement root = xdoc.Root; if (StringExtensions.HasValue(this.RootElement) && xdoc.Root != null) { root = xdoc.Root.Element(XmlExtensions.AsNamespaced(this.RootElement, this.Namespace)); } if (!StringExtensions.HasValue(this.Namespace)) { XmlDebuggingDeserializer.RemoveNamespace(xdoc); } T instance = Activator.CreateInstance <T>(); Type type = instance.GetType(); return(!ReflectionExtensions.IsSubclassOfRawGeneric(type, typeof(List <>)) ? (T)this.Map((object)instance, root) : (T)this.HandleListDerivative(root, type.Name, type)); }
private object HandleListDerivative(XElement root, string propName, Type type) { Type type1 = type.IsGenericType ? type.GetGenericArguments()[0] : type.BaseType.GetGenericArguments()[0]; IList list1 = (IList)Activator.CreateInstance(type); IList <XElement> list2 = (IList <XElement>)Enumerable.ToList <XElement>(root.Descendants(XmlExtensions.AsNamespaced(type1.Name, this.Namespace))); string name = type1.Name; DeserializeAsAttribute attribute = ReflectionExtensions.GetAttribute <DeserializeAsAttribute>(type1); if (attribute != null) { name = attribute.Name; } if (!Enumerable.Any <XElement>((IEnumerable <XElement>)list2)) { XName name1 = XmlExtensions.AsNamespaced(name.ToLower(), this.Namespace); list2 = (IList <XElement>)Enumerable.ToList <XElement>(root.Descendants(name1)); } if (!Enumerable.Any <XElement>((IEnumerable <XElement>)list2)) { XName name1 = XmlExtensions.AsNamespaced(StringExtensions.ToCamelCase(name, this.Culture), this.Namespace); list2 = (IList <XElement>)Enumerable.ToList <XElement>(root.Descendants(name1)); } if (!Enumerable.Any <XElement>((IEnumerable <XElement>)list2)) { list2 = (IList <XElement>)Enumerable.ToList <XElement>(Enumerable.Where <XElement>(root.Descendants(), (Func <XElement, bool>)(e => StringExtensions.RemoveUnderscoresAndDashes(e.Name.LocalName) == name))); } if (!Enumerable.Any <XElement>((IEnumerable <XElement>)list2)) { XName lowerName = XmlExtensions.AsNamespaced(name.ToLower(), this.Namespace); list2 = (IList <XElement>)Enumerable.ToList <XElement>(Enumerable.Where <XElement>(root.Descendants(), (Func <XElement, bool>)(e => (XName)StringExtensions.RemoveUnderscoresAndDashes(e.Name.LocalName) == lowerName))); } this.PopulateListFromElements(type1, (IEnumerable <XElement>)list2, list1); if (!type.IsGenericType) { this.Map((object)list1, root.Element(XmlExtensions.AsNamespaced(propName, this.Namespace)) ?? root); } return((object)list1); }
protected virtual XElement GetElementByName(XElement root, XName name) { XName name1 = XmlExtensions.AsNamespaced(name.LocalName.ToLower(), name.NamespaceName); XName name2 = XmlExtensions.AsNamespaced(StringExtensions.ToCamelCase(name.LocalName, this.Culture), name.NamespaceName); if (root.Element(name) != null) { return(root.Element(name)); } if (root.Element(name1) != null) { return(root.Element(name1)); } if (root.Element(name2) != null) { return(root.Element(name2)); } if (name == XmlExtensions.AsNamespaced("Value", name.NamespaceName)) { return(root); } return(Enumerable.FirstOrDefault <XElement>((IEnumerable <XElement>)Enumerable.OrderBy <XElement, int>(root.Descendants(), (Func <XElement, int>)(d => Enumerable.Count <XElement>(d.Ancestors()))), (Func <XElement, bool>)(d => StringExtensions.RemoveUnderscoresAndDashes(d.Name.LocalName) == name.LocalName)) ?? Enumerable.FirstOrDefault <XElement>((IEnumerable <XElement>)Enumerable.OrderBy <XElement, int>(root.Descendants(), (Func <XElement, int>)(d => Enumerable.Count <XElement>(d.Ancestors()))), (Func <XElement, bool>)(d => StringExtensions.RemoveUnderscoresAndDashes(d.Name.LocalName) == name.LocalName.ToLower()))); }
protected virtual object Map(object x, XElement root) { foreach (PropertyInfo prop in x.GetType().GetProperties()) { try { Type type = prop.PropertyType; if ((type.IsPublic || type.IsNestedPublic) && prop.CanWrite) { object[] customAttributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false); XName name = customAttributes.Length <= 0 ? XmlExtensions.AsNamespaced(prop.Name, this.Namespace) : XmlExtensions.AsNamespaced(((DeserializeAsAttribute)customAttributes[0]).Name, this.Namespace); object valueFromXml = this.GetValueFromXml(root, name, prop); if (valueFromXml == null) { if (type.IsGenericType) { Type t = type.GetGenericArguments()[0]; XElement elementByName = this.GetElementByName(root, (XName)t.Name); IList list = (IList)Activator.CreateInstance(type); if (elementByName != null && root != null) { IEnumerable <XElement> elements = root.Elements(elementByName.Name); this.PopulateListFromElements(t, elements, list); } prop.SetValue(x, (object)list, (object[])null); } } else { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (string.IsNullOrEmpty(valueFromXml.ToString())) { prop.SetValue(x, (object)null, (object[])null); continue; } type = type.GetGenericArguments()[0]; } if (type == typeof(bool)) { string s = valueFromXml.ToString().ToLower(); prop.SetValue(x, (object)(bool)(XmlConvert.ToBoolean(s) ? true : false), (object[])null); } else if (type.IsPrimitive) { prop.SetValue(x, ReflectionExtensions.ChangeType(valueFromXml, type, this.Culture), (object[])null); } else if (type.IsEnum) { object enumValue = ReflectionExtensions.FindEnumValue(type, valueFromXml.ToString(), this.Culture); prop.SetValue(x, enumValue, (object[])null); } else if (type == typeof(Uri)) { Uri uri = new Uri(valueFromXml.ToString(), UriKind.RelativeOrAbsolute); prop.SetValue(x, (object)uri, (object[])null); } else if (type == typeof(string)) { prop.SetValue(x, valueFromXml, (object[])null); } else if (type == typeof(DateTime)) { object obj = (object) (StringExtensions.HasValue(this.DateFormat) ? DateTime.ParseExact(valueFromXml.ToString(), this.DateFormat, (IFormatProvider)this.Culture) : DateTime.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture)); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(DateTimeOffset)) { string str = valueFromXml.ToString(); if (!string.IsNullOrEmpty(str)) { try { DateTimeOffset dateTimeOffset = XmlConvert.ToDateTimeOffset(str); prop.SetValue(x, (object)dateTimeOffset, (object[])null); } catch (Exception ex) { object result; if (XmlDebuggingDeserializer.TryGetFromString(str, out result, type)) { prop.SetValue(x, result, (object[])null); } else { DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(str); prop.SetValue(x, (object)dateTimeOffset, (object[])null); } } } } else if (type == typeof(Decimal)) { object obj = (object)Decimal.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(Guid)) { object obj = (object) (string.IsNullOrEmpty(valueFromXml.ToString()) ? Guid.Empty : new Guid(valueFromXml.ToString())); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(TimeSpan)) { TimeSpan timeSpan = XmlConvert.ToTimeSpan(valueFromXml.ToString()); prop.SetValue(x, (object)timeSpan, (object[])null); } else if (type.IsGenericType) { Type t = type.GetGenericArguments()[0]; IList list = (IList)Activator.CreateInstance(type); XElement elementByName = this.GetElementByName(root, XmlExtensions.AsNamespaced(prop.Name, this.Namespace)); if (elementByName.HasElements) { XElement xelement = Enumerable.FirstOrDefault <XElement>(elementByName.Elements()); if (xelement != null) { IEnumerable <XElement> elements = elementByName.Elements(xelement.Name); this.PopulateListFromElements(t, elements, list); } } prop.SetValue(x, (object)list, (object[])null); } else if (ReflectionExtensions.IsSubclassOfRawGeneric(type, typeof(List <>))) { object obj = this.HandleListDerivative(root, prop.Name, type); prop.SetValue(x, obj, (object[])null); } else { object result; if (XmlDebuggingDeserializer.TryGetFromString(valueFromXml.ToString(), out result, type)) { prop.SetValue(x, result, (object[])null); } else if (root != null) { XElement elementByName = this.GetElementByName(root, name); if (elementByName != null) { object andMap = this.CreateAndMap(type, elementByName); prop.SetValue(x, andMap, (object[])null); } } } } } } catch (Exception e) { throw; } } return(x); }
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); } } }