Converts an XML string to an object : Xml Serialization « XML « C# / C Sharp






Converts an XML string to an object

  
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml.Serialization;


namespace Utilities
{
    /// <summary>
    /// Utility class for managing files
    /// </summary>
    public static class FileManager
    {

        /// <summary>
        /// Gets a files' contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>a string containing the file's contents</returns>
        public static string GetFileContents(string FileName)
        {
            try
            {
                return GetFileContents(FileName, 5000);
            }
            catch { throw; }
        }

        /// <summary>
        /// Gets a files' contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <param name="TimeOut">Amount of time in ms to wait for the file</param>
        /// <returns>a string containing the file's contents</returns>
        public static string GetFileContents(string FileName, int TimeOut)
        {
            StreamReader Reader = null;
            int StartTime = System.Environment.TickCount;
            try
            {
                bool Opened = false;
                while (!Opened)
                {
                    try
                    {
                        if (System.Environment.TickCount - StartTime >= TimeOut)
                            throw new System.IO.IOException("File opening timed out");
                        Reader = File.OpenText(FileName);
                        Opened = true;
                    }
                    catch (System.IO.IOException e)
                    {
                        throw e;
                    }
                }
                string Contents = Reader.ReadToEnd();
                Reader.Close();
                return Contents;
            }
            catch
            {
                return "";
            }
            finally
            {
                if (Reader != null)
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
        }

    }
    /// <summary>
    /// Helps with serializing an object to XML and back again.
    /// </summary>
    public static class Serialization
    {


        /// <summary>
        /// Takes an XML file and exports the Object it holds
        /// </summary>
        /// <param name="FileName">File name to use</param>
        /// <param name="Object">Object to export to</param>
        /// <param name="ObjectType">Object type to export</param>
        public static void XMLToObject(string FileName, out object Object,Type ObjectType)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                throw new ArgumentException("File name can not be null/empty");
            }
            try
            {
                string FileContent = FileManager.GetFileContents(FileName);
                Object = XMLToObject(FileContent,ObjectType);
            }
            catch { throw; }
        }

        /// <summary>
        /// Converts an XML string to an object
        /// </summary>
        /// <param name="XML">XML string</param>
        /// <param name="ObjectType">Object type to export</param>
        /// <returns>The object of the specified type</returns>
        public static object XMLToObject(string XML,Type ObjectType)
        {
            if (string.IsNullOrEmpty(XML))
            {
                throw new ArgumentException("XML can not be null/empty");
            }
            try
            {
                using (MemoryStream Stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(XML)))
                {
                    XmlSerializer Serializer = new XmlSerializer(ObjectType);
                    return Serializer.Deserialize(Stream);
                }
            }
            catch { throw; }
        }

    }
}

   
    
  








Related examples in the same category

1.Serialize List of Objects
2.Use XmlSerializer
3.Set Xml Attribute when serilzation
4.Use XML Serialization with Custom Objects
5.XmlRootAttribute
6.Takes an XML file and exports the Object it holds
7.Deserializes/Serializes an xml document back into an object
8.Deserialize with XmlSerializer
9.XmlSerializer Util
10.Serilize to Xml
11.Xml Serialization Helper
12.Xml Serialization Helper 2
13.Tiny Xml Serializer
14.Xml Serialization Manager