Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Save object to file C#

/// <summary>
    /// Serializes an object.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serializableObject"></param>
    /// <param name="fileName"></param>
    public void SerializeObject<T>(T serializableObject, string fileName)
    {
        if (serializableObject == null) { return; }

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serializableObject);
                stream.Position = 0;
                xmlDocument.Load(stream);
                xmlDocument.Save(fileName);
            }
        }
        catch (Exception ex)
        {
            //Log exception here
        }
    }


    /// <summary>
    /// Deserializes an xml file into an object list
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public T DeSerializeObject<T>(string fileName)
    {
        if (string.IsNullOrEmpty(fileName)) { return default(T); }

        T objectOut = default(T);

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            string xmlString = xmlDocument.OuterXml;

            using (StringReader read = new StringReader(xmlString))
            {
                Type outType = typeof(T);

                XmlSerializer serializer = new XmlSerializer(outType);
                using (XmlReader reader = new XmlTextReader(read))
                {
                    objectOut = (T)serializer.Deserialize(reader);
                }
            }
        }
        catch (Exception ex)
        {
            //Log exception here
        }

        return objectOut;
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: label wpf 
Csharp :: c# generic return type in interface 
Csharp :: c# while loop 
Csharp :: c# find element in list of list 
Csharp :: c# get smallest of 3 numbers 
Csharp :: c# const 
Csharp :: c# webapi return file 
Csharp :: unity get audio clip length 
Csharp :: c# standard microphone decibels 
Csharp :: unity auto scroll 
Csharp :: render world space UI infront of everything unity 
Csharp :: itext7 pdfwriter outputstream c# 
Csharp :: c# list any retun indec 
Csharp :: telerik mvc grid column with icon 
Csharp :: C# traverseall elements in class property 
Csharp :: eventsource web api c# 
Csharp :: linq select max value from list 
Csharp :: asp net c# browser cursor wait 
Csharp :: pricipal permission attribute in c# 
Csharp :: c# loop through queue 
Csharp :: regex only letters and numbers c# 
Csharp :: unity sprite disappears when using transform.lookat 
Csharp :: browser folder in wpf 
Csharp :: Non-Serialized Fields in unity inspector 
Csharp :: c# check if string contains character multiple times 
Csharp :: c# get private property 
Csharp :: C# Async Function with await 
Csharp :: Get replace normal text from word document in C# 
Csharp :: C# [] overload 
Csharp :: get list of months and year between two dates c# 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =