Xml serialize

Base class for serialze the objec to an Xml file.

/// <summary>
    /// Object for reading and writing XML
    /// </summary>
    public class XMLbase<T>
    {
        /// <summary>
        /// Writes an object to an XML file
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="xmlObject">The object to serialize</param>
        /// <param name="location">The fysic location</param>
        /// <returns></returns>
        public virtual ReturnMessage WriteXML(T xmlObject, string location, string filename)
        {
            // validation
            if (xmlObject == null)
            {
                return ReturnMessage.GetReturnMessage(false, "Object to serialize cannot be null!", null);
            }
            if (string.IsNullOrEmpty(location))
            {
                return ReturnMessage.GetReturnMessage(false, "Location for writing XML cannot be empty!", null);
            }
            if (string.IsNullOrEmpty(filename))
            {
                return ReturnMessage.GetReturnMessage(false, "Name off the file for writing XML cannot be empty!", null);
            }

            // create filePath
            string fullPath = Path.Combine(location, filename);
            // serialize
            try
            {
                var serializer = new XmlSerializer(typeof(T));
                using (var fstream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    serializer.Serialize(fstream, xmlObject);
                }
            }
            catch (Exception excp)
            {
                return ReturnMessage.GetReturnMessage(false, string.Format("Error writing xml: {0}", excp.Message), excp);
            }

            return ReturnMessage.GetReturnMessage(true, string.Format("Object: {0} is serialized to xml-file{1}", xmlObject.GetType().Name, location), null);

        }

        /// <summary>
        /// Validates the XML file
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        public virtual ReturnMessage Validate(string location, string filename)
        {
            // validation
            if (string.IsNullOrEmpty(location)) { return ReturnMessage.GetReturnMessage(false, "Location for loading XML cannot be empty", null); };
            if (string.IsNullOrEmpty(filename)) { return ReturnMessage.GetReturnMessage(false, "file name for loading XML cannot be empty", null); };

            string fullPath = Path.Combine(location, filename);
            if (!File.Exists(fullPath)) { return ReturnMessage.GetReturnMessage(false, "The file in the specified location does not exist!", null); };
            
            var serializer = new XmlSerializer(typeof(T));
            using (var xmlReader = XmlReader.Create(fullPath))
            {
                bool succeeded = serializer.CanDeserialize(xmlReader);
                return ReturnMessage.GetReturnMessage(succeeded,string.Empty, null);
            }
        }

        /// <summary>
        /// Returns an object created from
        /// a XmlFile
        /// </summary>
        /// <param name="location">The location off the XML File</param>
        /// <returns></returns>
        public virtual T GetFromXml(string location, string filename)
        {
            // validation
            if (string.IsNullOrEmpty(location)) { throw new ArgumentNullException("Location for loading XML cannot be empty"); };

            string fullPath = Path.Combine(location, filename);
            if (!File.Exists(fullPath)) { throw new ArgumentNullException("The file in the specified location does not exist!"); };

            var serializer = new XmlSerializer(typeof(T));
            using (var xmlReader = XmlReader.Create(fullPath))
            {
                var result = (T)serializer.Deserialize(xmlReader);
                return result;
            }
        }
    } 

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!