Source

main / Source / WebService / Web.Common / Extensions / SerializerExtensions.cs

Naethra D ba1cb57 





































































































//-----------------------------------------------------------------------
// <copyright file="SerializerExtensions.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation 2011. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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

namespace DataUp.Web.Common
{
    /// <summary>
    /// Class having the extension methods needed for Serialization
    /// </summary>
    public static class SerializerExtensions
    {
        /// <summary>
        /// Serializes the XML content and returns the content as a memory stream.
        /// </summary>
        /// <returns>Stream having the xml string.</returns>
        public static Stream GetXmlStream<T>(this T thisObject)
        {
            MemoryStream stream = 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);
                }

                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(sb.ToString());
                stream = new MemoryStream();
                xmlDoc.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
            }
            catch (XmlException)
            {
                // Return null stream in case of exception.
            }

            return stream;
        }

        /// <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;
        }

        /// <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;
        }
    }
}
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.