Serialize() private static method

Serializes the specified property into a XElement using options.
private static Serialize ( PropertyInfo property, object parentObject, XmlConvertOptions options ) : System.Xml.Linq.XElement
property System.Reflection.PropertyInfo The property to serialize.
parentObject object The object that owns the property.
options XmlConvertOptions Indicates how the output is formatted or serialized.
return System.Xml.Linq.XElement
Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }