main / Source / Excel / Excel.Common / Extensions / SerializerExtensions.cs

using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace DataUp.Excel.Common
{
    /// <summary>
    /// Class having the extension methods needed for Serialization
    /// </summary>
    public static class SerializerExtensions
    {
        /// <summary>
        /// Gets the decoded version of the string.
        /// </summary>
        /// <param name="thisObject">Current string.</param>
        /// <returns>Decoded version of the string.</returns>
        public static T DeserializeXML<T>(this string thisObject)
        {
            T result = default(T);
            try
            {
                var x = new XmlSerializer(typeof(T));
                using (var xw = new StringReader(thisObject))
                {
                    result = (T)x.Deserialize(xw);
                }
            }
            catch (XmlException)
            {
                // Return null stream in case of exception.
            }

            return result;
        }

        /// <summary>
        /// Gets the decoded version of the string.
        /// </summary>
        /// <param name="thisObject">Current string.</param>
        /// <returns>Decoded version of the string.</returns>
        public static string SerializeToXML<T>(this T thisObject)
        {
            string result = string.Empty;
            try
            {
                var x = new XmlSerializer(typeof(T));
                using (var xw = new StringWriter(CultureInfo.InvariantCulture))
                {
                    x.Serialize(xw, thisObject);

                    result = xw.ToString();
                }
            }
            catch (XmlException)
            {
                // Return null stream in case of exception.
            }

            return result;
        }

        /// <summary>
        /// Serializes the XML content and returns the content as a string
        /// </summary>
        /// <returns>Serialized string.</returns>
        public static string SerializeToString<T>(this T thisObject)
        {
            string serializedString = null;
            try
            {
                var sb = new StringBuilder();
                var x = new XmlSerializer(typeof(T));
                using (var xw = XmlWriter.Create(sb))
                {
                    var emptyNamespace = new XmlSerializerNamespaces();
                    emptyNamespace.Add(string.Empty, string.Empty);
                    x.Serialize(xw, thisObject, emptyNamespace);
                }

                serializedString = sb.ToString();
            }
            catch (XmlException)
            {
                // Return null stream in case of exception.
            }

            return serializedString;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.