private static async Task ToXml(object obj, XmlWriter writer, SerializerOptions options = null) { var type = obj.GetType(); var typeInfo = type.GetTypeInfo(); if (typeInfo.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { await writer.WriteStringAsync(Convert.ToString(obj, CultureInfo.InvariantCulture)); } else if (typeInfo.IsEnum) { await writer.WriteStringAsync(Enum.GetName(type, obj)); } else if (type == typeof(DateTime)) { var format = DateFormatForOptions(options); await writer.WriteStringAsync(((DateTime)obj).ToString(format)); } else if (typeInfo.ImplementedInterfaces.Contains(typeof(IEnumerable))) { foreach (var val in (IEnumerable) obj) { await writer.WriteStartElementAsync(null, NameForType(val.GetType()), null); await ToXml(val, writer); await writer.WriteEndElementAsync(); } } else { foreach(var property in type.GetRuntimeProperties()) { SerializerOptions opt = null; var ignoreAttr = property.GetCustomAttribute<XmlIgnoreAttribute>(); var isStatic = property.GetMethod.IsStatic; if (ignoreAttr != null || isStatic) { continue; } var attr = property.GetCustomAttribute<XmlElementAttribute>(); if (attr != null) { opt = new SerializerOptions { DataType = attr.DataType, }; } var value = property.GetMethod.Invoke(obj, new object[0]); await writer.WriteStartElementAsync(null, property.Name, null); if (value != null) { await ToXmlNode(value, writer, opt); } await writer.WriteEndElementAsync(); } } }
private static async Task ToXmlNode(object obj, XmlWriter writer, SerializerOptions opt) { var writable = obj as IXmlWritable; if (writable != null) { await writable.WriteXml(writer); return; } await ToXml(obj, writer, opt); }
private static string DateFormatForOptions(SerializerOptions options) { if (options != null) { switch (options.DataType.ToLowerInvariant()) { case "date": return "yyyy-MM-dd"; case "time": return "HH:mm:ss"; } } return "yyyy-MM-ddTHH:mm:ss"; }
private static XElement ToXml(object obj, SerializerOptions options = null) { var type = obj.GetType(); var typeInfo = type.GetTypeInfo(); var el = new XElement(NameForType(type)); if(typeInfo.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { el.Value = Convert.ToString(obj, CultureInfo.InvariantCulture); } else if (typeInfo.IsEnum) { el.Value = Enum.GetName(type, obj); } else if (type == typeof(DateTime)) { var format = DateFormatForOptions(options); el.Value = ((DateTime) obj).ToString(format); } else if (typeInfo.ImplementedInterfaces.Contains(typeof(IEnumerable))) { foreach (var val in (IEnumerable) obj) { el.Add(ToXml(val)); } } else { foreach(var property in type.GetRuntimeProperties()) { SerializerOptions opt = null; var ignoreAttr = property.GetCustomAttribute<XmlIgnoreAttribute>(); var isStatic = property.GetMethod.IsStatic; if (ignoreAttr != null || isStatic) { continue; } var attr = property.GetCustomAttribute<XmlElementAttribute>(); if (attr != null) { opt = new SerializerOptions { DataType = attr.DataType, }; } var value = property.GetMethod.Invoke(obj, new object[0]); var resultElement = new XElement(property.Name); if (value != null) { var element = ToXml(value, opt); if (element.HasElements) { resultElement.Add(element.Elements()); } else { resultElement.Value = element.Value; } } el.Add(resultElement); } } return el; }
private static XElement ToXml(object obj, SerializerOptions options = null) { var type = obj.GetType(); var typeInfo = type.GetTypeInfo(); var el = new XElement(NameForType(type)); if (typeInfo.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { el.Value = Convert.ToString(obj, CultureInfo.InvariantCulture); } else if (typeInfo.IsEnum) { el.Value = Enum.GetName(type, obj); } else if (type == typeof(DateTime)) { var format = DateFormatForOptions(options); el.Value = ((DateTime)obj).ToString(format); } else if (typeInfo.ImplementedInterfaces.Contains(typeof(IEnumerable))) { foreach (var val in (IEnumerable)obj) { el.Add(ToXml(val)); } } else { foreach (var property in type.GetRuntimeProperties()) { SerializerOptions opt = null; var ignoreAttr = property.GetCustomAttribute <XmlIgnoreAttribute>(); var isStatic = property.GetMethod.IsStatic; if (ignoreAttr != null || isStatic) { continue; } var attr = property.GetCustomAttribute <XmlElementAttribute>(); if (attr != null) { opt = new SerializerOptions { DataType = attr.DataType, }; } var value = property.GetMethod.Invoke(obj, new object[0]); var resultElement = new XElement(property.Name); if (value != null) { var element = ToXml(value, opt); if (element.HasElements) { resultElement.Add(element.Elements()); } else { resultElement.Value = element.Value; } } el.Add(resultElement); } } return(el); }