Serialization

  • Thread starter Thread starter John S
  • Start date Start date
J

John S

I understand what serialization is (saving values to a file).
Can someone give me a quick, easy-to-understand example of using
serialization?
Please do not send me the microsoft example. I've seen it and it stinks.
 
I understand what serialization is (saving values to a file).
Can someone give me a quick, easy-to-understand example of using
serialization?
Please do not send me the microsoft example. I've seen it and it stinks.
I have a config file which the user can change. It is represented in my
system as a class with properties for each config option. At start up,
I load it from disc:
XmlTextReader t = null;
try {
t = new XmlTextReader(path);
XmlSerializer s = new XmlSerializer(typeof(NewsLookSystem));
newSystem = (NewsLookSystem)s.Deserialize(t);
} catch {
t.Close();
newSystem = new NewsLookSystem();
} finally {
if (t != null) t.Close();
}
return newSystem;

At various times, I save it to disc:

TextWriter t = null;
try {
string configFile = Bootstrap.ConfigFolder + "\\NewsLookSystem.xml";
t = new StreamWriter(configFile);
XmlSerializer s = new XmlSerializer(typeof(NewsLookSystem));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("NewsLookSystem", "urn:svs-NewsLook");
s.Serialize(t, newSystem, ns);
} catch (Exception exc) {
Console.WriteLine(exc.Message);
} finally {
if (t != null) t.Close();
}


Is that a better example?
 
Back
Top