Exemple #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Deserializes XML from the specified string to an object of the specified type.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public static T DeserializeFromString <T>(string input, bool fKeepWhitespaceInElements,
                                                  out Exception e) where T :
        class
        {
            T data = null;

            e = null;

            try {
                if (string.IsNullOrEmpty(input))
                {
                    return(null);
                }

                // Whitespace is not allowed before the XML declaration,
                // so get rid of any that exists.
                input = input.TrimStart();

                using (InternalXmlReader reader = new InternalXmlReader(
                           new StringReader(input), fKeepWhitespaceInElements)) {
                    data = DeserializeInternal <T>(reader, null);
                }
            } catch (Exception outEx) {
                e = outEx;
            }

            return(data);
        }
        /// --------------------------------------------------------------------------------
        /// <summary>
        /// Deserializes XML from the specified file to an object of the specified type.
        /// </summary>
        /// <typeparam name="T">The object type</typeparam>
        /// <param name="filename">The filename from which to load</param>
        /// <param name="rootElementName">Name to expect for the root element. This is
        /// good when T is a generic list of some type (e.g. List of strings).</param>
        /// <param name="fKeepWhitespaceInElements">if set to <c>true</c>, the reader
        /// will preserve and return elements that contain only whitespace, otherwise
        /// these elements will be ignored during a deserialization.</param>
        /// <param name="e">The exception generated during the deserialization.</param>
        /// <returns></returns>
        /// --------------------------------------------------------------------------------
        public static T DeserializeFromFile <T>(string filename, string rootElementName,
                                                bool fKeepWhitespaceInElements, out Exception e)
        {
            T data = default(T);

            e = null;

            try
            {
                if (!File.Exists(filename))
                {
                    return(default(T));
                }

                using (InternalXmlReader reader = new InternalXmlReader(
                           filename, fKeepWhitespaceInElements))
                {
                    data = DeserializeInternal <T>(reader, rootElementName);
                }
            }
            catch (Exception outEx)
            {
                e = outEx;
            }

            return(data);
        }