Exemplo n.º 1
0
        /// ------------------------------------------------------------------------------------
        /// <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="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>
        /// ------------------------------------------------------------------------------------
        public static T DeserializeFromFile <T>(string filename, bool fKeepWhitespaceInElements,
                                                out Exception e) where T : class
        {
            T data = null;

            e = null;

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

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

            return(data);
        }
Exemplo n.º 2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Deserializes an object using the specified reader.
        /// </summary>
        /// <typeparam name="T">The type of object to deserialize</typeparam>
        /// <param name="reader">The reader.</param>
        /// <returns>The deserialized object</returns>
        /// ------------------------------------------------------------------------------------
        private static T DeserializeInternal <T>(XmlScrTextReader reader)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));

            deserializer.UnknownAttribute += deserializer_UnknownAttribute;
            return((T)deserializer.Deserialize(reader));
        }
Exemplo n.º 3
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 (XmlScrTextReader reader = new XmlScrTextReader(
                           new StringReader(input), fKeepWhitespaceInElements))
                {
                    data = DeserializeInternal <T>(reader);
                }
            }
            catch (Exception outEx)
            {
                e = outEx;
            }

            return(data);
        }