Пример #1
0
        /// <summary>
        /// Serializeas a list of objects
        /// </summary>
        /// <param name="_object">The object to serialize</param>
        /// <param name="_filePath">path where the xml file should be located</param>
        /// <returns></returns>
        public static void Serialize(Object _object, String _filePath)
        {
            // XML objects
            XmlWriter     xmlWriter     = null;
            XmlSerializer xmlSerializer = null;

            Type[] propertyTypes = null;

            try
            {
                //Create XMLWriter with the specific path and ItemName
                xmlWriter = XmlWriter.Create(_filePath);
                //xmlWriter.Settings.Encoding = Encoding.UTF8;

                propertyTypes = ReflectionHandler.GetPropertyTypes(_object).ToArray();

                // Serialize the object
                xmlSerializer = new XmlSerializer(_object.GetType(), propertyTypes);
                xmlSerializer.Serialize(xmlWriter, _object);
            }

            catch (Exception exception)
            {
                throw exception;
            }

            finally
            {
                if (xmlWriter != null)
                {
                    // Close the XMLWriter
                    xmlWriter.Close();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Serializeas a list of objects
        /// </summary>
        /// <param name="_object">The object to serialize</param>
        /// <param name="_filePath">path where the xml file should be located</param>
        /// <returns></returns>
        public static Boolean TrySerialize(Object _object, String _filePath)
        {
            // XML objects
            XmlWriter     xmlWriter;
            XmlSerializer xmlSerializer;

            Type[] propertyTypes;

            //Create XMLWriter with the specific path and ItemName
            xmlWriter = XmlWriter.Create(_filePath);

            try
            {
                propertyTypes = ReflectionHandler.GetPropertyTypes(_object).ToArray();

                // Serialize the object
                xmlSerializer = new XmlSerializer(_object.GetType(), propertyTypes);
                xmlSerializer.Serialize(xmlWriter, _object);
            }

            catch
            {
                return(false);
            }

            finally
            {
                // Close the XMLWriter
                xmlWriter.Close();
            }

            return(true);
        }