Try this code to store your object in xml format, you can dump and read that to a simple file.txt
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Put.Here.Your.Proyects.Namespace
{
public class Serializer
{
public static string Serialize(Type typeOfObj, object obj)
{
Stream memStm = new MemoryStream();
StreamWriter tw = new StreamWriter(memStm);
XmlSerializer serializadorXML = new XmlSerializer(typeOfObj);
serializadorXML.Serialize(tw, obj);
memStm.Position = 0;
StreamReader stmRdr = new StreamReader(memStm);
string text = stmRdr.ReadToEnd();
tw.Close();
return text.Trim();
}
public static object Deserialize(Type typeOfObj, string text)
{
object obj = null;
byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream stream = new MemoryStream(byteArray);
XmlReader xmlReader = XmlReader.Create(stream);
XmlSerializer serializadorXML = new XmlSerializer(typeOfObj);
if (serializadorXML.CanDeserialize(xmlReader))
{
obj = serializadorXML.Deserialize(xmlReader);
}
return obj;
}
}
}
to serialize use
typeof
like example:
string xmlText = Serialize(typeOf miObjectClassName, myObject);
to deserealize cast object:
miObjectClassName myObject = (miObjectClassName)Deserialize(typeOf miObjectClassName, xmlText);
you can remove
static
label to fuction but I recomend not because you can use fuction like utility without need to create object:
string x = Put.Here.Your.Proyects.Namespace.Serialize(typeOf miObjectClassName, myObject);