/// <summary> /// Deserializes the XElement to the object of a specified type using options. /// </summary> /// <param name="type">The type of the object to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the object.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { var value = parentElement?.Value; if (value == null) { return(null); } //We might not have an element for a property var objectType = ObjectType.From(type); if (objectType == ObjectType.Primitive) { return(PrimitiveSerializer.Deserialize(type, parentElement, options)); } else if (objectType == ObjectType.Dictionary) { return(DictionarySerializer.Deserialize(type, parentElement, options)); } else if (objectType == ObjectType.List) { return(ListSerializer.Deserialize(type, parentElement, options)); } else { return(DeserializeObject(type, parentElement, options)); } }
/// <summary> /// Deserializes the XElement to the list (e.g. List<T>, Array of a specified type using options. /// </summary> /// <param name="type">The type of the list to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the list.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized list from the XElement.</returns> public static object Deserialize(Type type, XElement parent, XmlConvertOptions options) { IList list; if (type.GetTypeInfo().IsInterface) { list = new List<object>(); } else { list = (IList)Activator.CreateInstance(type); } var elements = parent.Elements(); foreach (var element in elements) { var elementType = Utilities.GetElementType(element, type, 0); if (elementType != null) { var obj = ObjectSerializer.Deserialize(elementType, element, options); list.Add(obj); } else { throw new InvalidOperationException("Could not deserialize this non generic dictionary without more type information."); } } return list; }
/// <summary> /// Deserializes the XElement to the list (e.g. List<T>, Array of a specified type using options. /// </summary> /// <param name="type">The type of the list to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the list.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized list from the XElement.</returns> public static object Deserialize(Type type, XElement parent, XmlConvertOptions options) { IList list; if (type.GetTypeInfo().IsInterface) { list = new List <object>(); } else { list = (IList)Activator.CreateInstance(type); } var elements = parent.Elements(); foreach (var element in elements) { var elementType = Utilities.GetElementType(element, type, 0); if (elementType != null) { var obj = ObjectSerializer.Deserialize(elementType, element, options); list.Add(obj); } else { throw new InvalidOperationException("Could not deserialize this non generic dictionary without more type information."); } } return(list); }
/// <summary> /// Serializes a fundamental primitive object (e.g. string, int etc.) into a XElement using options. /// </summary> /// <param name="value">The primitive to serialize.</param> /// <param name="name">The name of the primitive to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the primitive.</returns> public static XElement Serialize(object value, string name, XmlConvertOptions options) { var stringValue = Convert.ToString(value); var element = new XElement(name, stringValue); return element; }
/// <summary> /// Serializes a fundamental primitive object (e.g. string, int etc.) into a XElement using options. /// </summary> /// <param name="value">The primitive to serialize.</param> /// <param name="name">The name of the primitive to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the primitive.</returns> public static XElement Serialize(object value, string name, XmlConvertOptions options) { var stringValue = Convert.ToString(value); var element = new XElement(name, stringValue); return(element); }
/// <summary> /// Deserializes the XML string to the specified .NET type using options. /// </summary> /// <param name="type">The type of the deserialized .NET object.</param> /// <param name="xml">The XML string to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XML string.</returns> public static object DeserializeObject(Type type, string xml, XmlConvertOptions options) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } return(DeserializeXElement(type, XElement.Parse(xml), options)); }
/// <summary> /// Serializes the specified object to a XElement using options. /// Note: properties without both a getter AND setter will be ignored. Add a private setter if you wish to include the property in the XML output. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the object.</returns> public static XElement SerializeXElement(object value, XmlConvertOptions options) { if (value == null) { throw new ArgumentNullException(nameof(value)); } var identifier = Utilities.GetIdentifier(value); return(ObjectSerializer.Serialize(value, identifier, options)); }
/// <summary> /// Deserializes the XElement to the specified .NET type using options. /// </summary> /// <param name="type">The type of the deserialized .NET object.</param> /// <param name="element">The XElement to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static object DeserializeXElement(Type type, XElement element, XmlConvertOptions options) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (element == null) { throw new ArgumentNullException(nameof(element)); } return(ObjectSerializer.DeserializeObject(type, element, options)); }
/// <summary> /// Serializes a list (e.g. List<T>, Array etc.) into a XElement using options. /// </summary> /// <param name="value">The list to serialize.</param> /// <param name="name">The name of the list to serialize.</param> /// <param name="elementNames">The custom name of collection elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the list.</returns> public static XElement Serialize(object value, string name, string elementNames, XmlConvertOptions options) { var parentElement = new XElement(name); var list = (ICollection)value; foreach (var childValue in list) { var childElement = ObjectSerializer.Serialize(childValue, elementNames, null, null, null, options); Utilities.AddChildElement(childElement, parentElement); } return parentElement; }
/// <summary> /// Deserializes the XElement to the specified .NET type using options. /// </summary> /// <param name="type">The type of the deserialized .NET object.</param> /// <param name="element">The XElement to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static object DeserializeObject(Type type, XElement element, XmlConvertOptions options) { var value = Activator.CreateInstance(type); var identifier = Utilities.GetIdentifier(value); var properties = type.GetTypeInfo().DeclaredProperties; foreach (var property in properties) { DeserializeProperty(property, value, element, options); } return(value); }
/// <summary> /// Serializes the specified object to a XElement using options. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="name">The name of the object to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the object.</returns> public static XElement Serialize(object value, string name, XmlConvertOptions options) { var objectElement = new XElement(name); var properties = value.GetType().GetTypeInfo().DeclaredProperties; foreach (var property in properties) { var propertyElement = Serialize(property, value, options); Utilities.AddChildElement(propertyElement, objectElement); } return objectElement; }
/// <summary> /// Serializes the specified object to a XElement using options. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="name">The name of the object to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the object.</returns> public static XElement Serialize(object value, string name, XmlConvertOptions options) { var objectElement = new XElement(name); var properties = value.GetType().GetTypeInfo().DeclaredProperties; foreach (var property in properties) { var propertyElement = Serialize(property, value, options); Utilities.AddChildElement(propertyElement, objectElement); } return(objectElement); }
/// <summary> /// Deserializes the XElement to the dictionary (e.g. Dictionary<TKey, TValue>, HashTable of a specified type using options. /// </summary> /// <param name="type">The type of the dictionary to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the dictionary.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized dictionary from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { IDictionary dictionary; if (type.GetTypeInfo().IsInterface) { dictionary = new Dictionary <object, object>(); } else { dictionary = (IDictionary)Activator.CreateInstance(type); } var elements = parentElement.Elements(); foreach (var element in elements) { var keyValueElements = new List <XElement>(element.Elements()); if (keyValueElements.Count < 2) { //No fully formed key value pair continue; } var keyElement = keyValueElements[0]; var valueElement = keyValueElements[1]; var keyType = Utilities.GetElementType(keyElement, type, 0); var valueType = Utilities.GetElementType(valueElement, type, 1); if (keyType != null && valueType != null) { var key = ObjectSerializer.Deserialize(keyType, keyElement, options); var value = ObjectSerializer.Deserialize(valueType, valueElement, options); dictionary.Add(key, value); } else { throw new InvalidOperationException("Could not deserialize this non generic dictionary without more type information."); } } return(dictionary); }
/// <summary> /// Deserializes the XElement to the dictionary (e.g. Dictionary /// <TKey, TValue>, HashTable of a specified type using options. /// </summary> /// <param name="type">The type of the dictionary to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the dictionary.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized dictionary from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { IDictionary dictionary; if (type.GetTypeInfo().IsInterface) { dictionary = new Dictionary<object, object>(); } else { dictionary = (IDictionary) Activator.CreateInstance(type); } var elements = parentElement.Elements(); foreach (var element in elements) { var keyValueElements = new List<XElement>(element.Elements()); if (keyValueElements.Count < 2) { //No fully formed key value pair continue; } var keyElement = keyValueElements[0]; var valueElement = keyValueElements[1]; var keyType = Utilities.GetElementType(keyElement, type, 0); var valueType = Utilities.GetElementType(valueElement, type, 1); if (keyType != null && valueType != null) { var key = ObjectSerializer.Deserialize(keyType, keyElement, options); var value = ObjectSerializer.Deserialize(valueType, valueElement, options); dictionary.Add(key, value); } else { throw new InvalidOperationException( "Could not deserialize this non generic dictionary without more type information."); } } return dictionary; }
public static void SerializeDeserializeObject_Equal_Success <T>(T obj1, XmlConvertOptions options) where T : new() { T obj2 = default(T); T obj3 = default(T); string xml = null; // Serializing and deserializing strings if (options == XmlConvertOptions.None) { xml = XmlConvert.SerializeObject(obj1); obj2 = XmlConvert.DeserializeObject <T>(xml); obj3 = (T)XmlConvert.DeserializeObject(obj1.GetType(), xml); } else { xml = XmlConvert.SerializeObject(obj1, options); obj2 = XmlConvert.DeserializeObject <T>(xml, options); obj3 = (T)XmlConvert.DeserializeObject(obj1.GetType(), xml, options); } Assert.Equal(obj1, obj2); Assert.Equal(obj1, obj3); // Serializing and deserializing XElements XElement element = null; if (options == XmlConvertOptions.None) { element = XmlConvert.SerializeXElement(obj1); obj2 = XmlConvert.DeserializeXElement <T>(element); obj3 = (T)XmlConvert.DeserializeXElement(obj1.GetType(), element); } else { element = XmlConvert.SerializeXElement(obj1, options); obj2 = XmlConvert.DeserializeXElement <T>(element, options); obj3 = (T)XmlConvert.DeserializeXElement(obj1.GetType(), element, options); } Assert.Equal(obj1, obj2); Assert.Equal(obj1, obj3); }
/// <summary> /// Serializes a dictionary (e.g. List<TKey, TValue>, HashTable etc.) into a XElement using options. /// </summary> /// <param name="value">The dictionary to serialize.</param> /// <param name="name">The name of the dictionary to serialize.</param> /// <param name="elementNames">The custom name of collection elements.</param> /// <param name="keyNames">The optional custom name of dictionary key elements.</param> /// <param name="valueNames">The optional custom name of dictionary value elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the dictionary.</returns> public static XElement Serialize(object value, string name, string elementNames, string keyNames, string valueNames, XmlConvertOptions options) { var element = new XElement(name); var dictionary = (IDictionary)value; foreach (DictionaryEntry dictionaryEntry in dictionary) { var keyValueElement = new XElement(elementNames); var keyElement = ObjectSerializer.Serialize(dictionaryEntry.Key, keyNames, null, null, null, options); var valueElement = ObjectSerializer.Serialize(dictionaryEntry.Value, valueNames, null, null, null, options); Utilities.AddChildElement(keyElement, keyValueElement); Utilities.AddChildElement(valueElement, keyValueElement); element.Add(keyValueElement); } return element; }
/// <summary> /// Serializes the specified property into a XElement using options. /// </summary> /// <param name="property">The property to serialize.</param> /// <param name="parentObject">The object that owns the property.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns> /// The XElement representation of the property. May be null if it has no value, cannot be read or written or /// should be ignored. /// </returns> private static XElement Serialize(PropertyInfo property, object parentObject, XmlConvertOptions options) { if (Utilities.ShouldIgnoreProperty(property)) //Either we ignore or can't read the property { return null; } var m = property.GetMethod; var propertyValue = m.Invoke(parentObject, null); if (propertyValue == null) //Ignore null properties { return null; } var propertyName = Utilities.GetIdentifier(property); string elementNames = null; string keyNames = null; string valueNames = null; var objectType = ObjectType.From(property.PropertyType); if (objectType == ObjectType.Dictionary) { elementNames = Utilities.GetCollectionElementName(property); var dictionaryNames = Utilities.GetDictionaryElementName(property); keyNames = dictionaryNames.Key; valueNames = dictionaryNames.Value; } else if (objectType == ObjectType.List) { elementNames = Utilities.GetCollectionElementName(property); } return Serialize(propertyValue, propertyName, elementNames, keyNames, valueNames, options); }
/// <summary> /// Deserializes the XElement to the specified property using options. /// </summary> /// <param name="property">The property to deserialize the XElement into.</param> /// <param name="parentObject">The object that owns the property.</param> /// <param name="parentElement">The parent XElement used to deserialize the property.</param> /// <param name="options">Indicates how the output is deserialized.</param> private static void DeserializeProperty(PropertyInfo property, object parentObject, XElement parentElement, XmlConvertOptions options) { var name = Utilities.GetIdentifier(property); var type = property.PropertyType; var propertyElement = Utilities.GetChildElement(name, parentElement); var value = Deserialize(type, propertyElement, options); if (value != null) { property.SetValue(parentObject, value, null); } else { //Handle not parsable error } }
/// <summary> /// Serializes the specified object to a XML string using options. /// Note: properties without both a getter AND setter will be ignored. Add a private setter if you wish to include the property in the XML output. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XML string representation of the object.</returns> public static string SerializeObject(object value, XmlConvertOptions options) { return(SerializeXElement(value, options).ToString()); }
/// <summary> /// Serializes the specified property into a XElement using options. /// </summary> /// <param name="property">The property to serialize.</param> /// <param name="parentObject">The object that owns the property.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the property. May be null if it has no value, cannot be read or written or should be ignored.</returns> private static XElement Serialize(PropertyInfo property, object parentObject, XmlConvertOptions options) { if (Utilities.ShouldIgnoreProperty(property)) //Either we ignore or can't read the property { return(null); } var m = property.GetMethod; var propertyValue = m.Invoke(parentObject, null); if (propertyValue == null) //Ignore null properties { return(null); } var propertyName = Utilities.GetIdentifier(property); string elementNames = null; string keyNames = null; string valueNames = null; var objectType = ObjectType.From(property.PropertyType); if (objectType == ObjectType.Dictionary) { elementNames = Utilities.GetCollectionElementName(property); var dictionaryNames = Utilities.GetDictionaryElementName(property); keyNames = dictionaryNames.Key; valueNames = dictionaryNames.Value; } else if (objectType == ObjectType.List) { elementNames = Utilities.GetCollectionElementName(property); } return(Serialize(propertyValue, propertyName, elementNames, keyNames, valueNames, options)); }
/// <summary> /// Deserializes the XElement to the specified property using options. /// </summary> /// <param name="property">The property to deserialize the XElement into.</param> /// <param name="parentObject">The object that owns the property.</param> /// <param name="parentElement">The parent XElement used to deserialize the property.</param> /// <param name="options">Indicates how the output is deserialized.</param> private static void DeserializeProperty(PropertyInfo property, object parentObject, XElement parentElement, XmlConvertOptions options) { var name = Utilities.GetIdentifier(property); var type = property.PropertyType; var propertyElement = Utilities.GetChildElement(name, parentElement); var value = Deserialize(type, propertyElement, options); if (value != null) { property.SetValue(parentObject, value, null); } }
/// <summary> /// Deserializes the XElement to the specified .NET type using options. /// </summary> /// <param name="type">The type of the deserialized .NET object.</param> /// <param name="element">The XElement to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static object DeserializeObject(Type type, XElement element, XmlConvertOptions options) { var value = Activator.CreateInstance(type); var identifier = Utilities.GetIdentifier(value); var properties = type.GetTypeInfo().DeclaredProperties; foreach (var property in properties) { DeserializeProperty(property, value, element, options); } return value; }
/// <summary> /// Deserializes the XElement to the fundamental primitive (e.g. string, int etc.) of a specified type using options. /// </summary> /// <param name="type">The type of the fundamental primitive to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the fundamental primitive.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized fundamental primitive from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { return(Convert.ChangeType(parentElement.Value, type)); }
/// <summary> /// Deserializes the XElement to the fundamental primitive (e.g. string, int etc.) of a specified type using options. /// </summary> /// <param name="type">The type of the fundamental primitive to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the fundamental primitive.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized fundamental primitive from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { return Convert.ChangeType(parentElement.Value, type); }
/// <summary> /// Deserializes the XML string to the specified .NET type using options. /// </summary> /// <typeparam name="T">The type of the deserialized .NET object.</typeparam> /// <param name="xml">The XML string to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XML string.</returns> public static T DeserializeObject <T>(string xml, XmlConvertOptions options) where T : new() { return((T)DeserializeObject(typeof(T), xml, options)); }
/// <summary> /// Formats a serialized XElement with options. /// </summary> /// <param name="value">The object serialized.</param> /// <param name="element">The serialized XElement of the object.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement that was formatted with options. </returns> public static XElement SetupSerializedElement(object value, XElement element, XmlConvertOptions options) { if (!options.HasFlag(XmlConvertOptions.ExcludeTypes)) { var typeName = value.GetType().FullName; element.Add(new XAttribute("Type", typeName)); } return(element); }
/// <summary> /// Serializes a list (e.g. List<T>, Array etc.) into a XElement using options. /// </summary> /// <param name="value">The list to serialize.</param> /// <param name="name">The name of the list to serialize.</param> /// <param name="elementNames">The custom name of collection elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the list.</returns> public static XElement Serialize(object value, string name, string elementNames, XmlConvertOptions options) { var parentElement = new XElement(name); var list = (ICollection)value; foreach (var childValue in list) { var childElement = ObjectSerializer.Serialize(childValue, elementNames, null, null, null, options); Utilities.AddChildElement(childElement, parentElement); } return(parentElement); }
/// <summary> /// Formats a serialized XElement with options. /// </summary> /// <param name="value">The object serialized.</param> /// <param name="element">The serialized XElement of the object.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement that was formatted with options. </returns> public static XElement SetupSerializedElement(object value, XElement element, XmlConvertOptions options) { if (!options.HasFlag(XmlConvertOptions.ExcludeTypes)) { var typeName = value.GetType().FullName; element.Add(new XAttribute("Type", typeName)); } return element; }
/// <summary> /// Serializes the specified property into a XElement using options. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="name">The name of the object to serialize.</param> /// <param name="parentElement">The element in which to serialize the object.</param> /// <param name="elementNames">The optional custom name of collection elements.</param> /// <param name="keyNames">The optional custom name of dictionary key elements.</param> /// <param name="valueNames">The optional custom name of dictionary value elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the object.</returns> public static XElement Serialize(object value, string name, string elementNames, string keyNames, string valueNames, XmlConvertOptions options) { XElement element = null; var objectType = ObjectType.From(value); if (objectType == ObjectType.Primitive) { Debug.Assert(objectType != ObjectType.Other); //For 100% code coverage :/ element = PrimitiveSerializer.Serialize(value, name, options); } else if (objectType == ObjectType.Dictionary) { if (elementNames == null) { elementNames = "Element"; } if (keyNames == null) { keyNames = "Key"; } if (valueNames == null) { valueNames = "Value"; } element = DictionarySerializer.Serialize(value, name, elementNames, keyNames, valueNames, options); } else if (objectType == ObjectType.List) { if (elementNames == null) { elementNames = "Element"; } element = ListSerializer.Serialize(value, name, elementNames, options); } else { element = Serialize(value, name, options); //Recurse } return(Utilities.SetupSerializedElement(value, element, options)); }
/// <summary> /// Deserializes the XElement to the object of a specified type using options. /// </summary> /// <param name="type">The type of the object to deserialize.</param> /// <param name="parentElement">The parent XElement used to deserialize the object.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static object Deserialize(Type type, XElement parentElement, XmlConvertOptions options) { var value = parentElement?.Value; if (value == null) { return null; } //We might not have an element for a property var objectType = ObjectType.From(type); if (objectType == ObjectType.Primitive) { return PrimitiveSerializer.Deserialize(type, parentElement, options); } if (objectType == ObjectType.Dictionary) { return DictionarySerializer.Deserialize(type, parentElement, options); } if (objectType == ObjectType.List) { return ListSerializer.Deserialize(type, parentElement, options); } return DeserializeObject(type, parentElement, options); }
/// <summary> /// Deserializes the XElement to the specified .NET type using options. /// </summary> /// <typeparam name="T">The type of the deserialized .NET object.</typeparam> /// <param name="element">The XElement to deserialize.</param> /// <param name="options">Indicates how the output is deserialized.</param> /// <returns>The deserialized object from the XElement.</returns> public static T DeserializeXElement <T>(XElement element, XmlConvertOptions options) { return((T)DeserializeXElement(typeof(T), element, options)); }
/// <summary> /// Serializes the specified property into a XElement using options. /// </summary> /// <param name="value">The object to serialize.</param> /// <param name="name">The name of the object to serialize.</param> /// <param name="parentElement">The element in which to serialize the object.</param> /// <param name="elementNames">The optional custom name of collection elements.</param> /// <param name="keyNames">The optional custom name of dictionary key elements.</param> /// <param name="valueNames">The optional custom name of dictionary value elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the object.</returns> public static XElement Serialize(object value, string name, string elementNames, string keyNames, string valueNames, XmlConvertOptions options) { XElement element = null; var objectType = ObjectType.From(value); if (objectType == ObjectType.Primitive) { Debug.Assert(objectType != ObjectType.Other); //For 100% code coverage :/ element = PrimitiveSerializer.Serialize(value, name, options); } else if (objectType == ObjectType.Dictionary) { if (elementNames == null) { elementNames = "Element"; } if (keyNames == null) { keyNames = "Key"; } if (valueNames == null) { valueNames = "Value"; } element = DictionarySerializer.Serialize(value, name, elementNames, keyNames, valueNames, options); } else if (objectType == ObjectType.List) { if (elementNames == null) { elementNames = "Element"; } element = ListSerializer.Serialize(value, name, elementNames, options); } else { element = Serialize(value, name, options); //Recurse } return Utilities.SetupSerializedElement(value, element, options); }
/// <summary> /// Serializes a dictionary (e.g. List<TKey, TValue>, HashTable etc.) into a XElement using options. /// </summary> /// <param name="value">The dictionary to serialize.</param> /// <param name="name">The name of the dictionary to serialize.</param> /// <param name="elementNames">The custom name of collection elements.</param> /// <param name="keyNames">The optional custom name of dictionary key elements.</param> /// <param name="valueNames">The optional custom name of dictionary value elements.</param> /// <param name="options">Indicates how the output is formatted or serialized.</param> /// <returns>The XElement representation of the dictionary.</returns> public static XElement Serialize(object value, string name, string elementNames, string keyNames, string valueNames, XmlConvertOptions options) { var element = new XElement(name); var dictionary = (IDictionary)value; foreach (DictionaryEntry dictionaryEntry in dictionary) { var keyValueElement = new XElement(elementNames); var keyElement = ObjectSerializer.Serialize(dictionaryEntry.Key, keyNames, null, null, null, options); var valueElement = ObjectSerializer.Serialize(dictionaryEntry.Value, valueNames, null, null, null, options); Utilities.AddChildElement(keyElement, keyValueElement); Utilities.AddChildElement(valueElement, keyValueElement); element.Add(keyValueElement); } return(element); }