コード例 #1
0
        /// <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));
        }