But wait - why does the world really need another XML serialization API? What about the XmlSerializer, and BinarySerializer, and DataContractSerializer, and the dozens of serialization libraries on Google Code and elsewhere? And, for that matter, what about the AppSettings class built into .Net?
Many serialization solutions depend heavily on reflection often get bogged down with reflection details, leading to unexpected complexity, difficult debugging, and reduced readabiltiy of output. XMLConfig doesn't rely so heavily on reflection, and therefore requires the developer to configure objects to be writable. However, XMLConfig makes this writability as easy as possible. (The goals were: (1) make XMLConfig work intuitively, and (2) allow every interaction to occur in a single, easy function call.)
In order to make this class writable:
class SampleClass { int i; string list; SampleSubclass ss; }...just do this:
class SampleClass : IWritable { int i; List(Why two properties in each writer class? Because you don't HAVE to store the classes in the XML with the same names as the members. You can call them anything you want [using an alphanumeric string without whitespaces!), and then retrieve them in the reader using whatever name you like.)list; SampleSubclass ss; public SampleClass() { } // must have a parameterless constructor public void Initialize(Dictionary dObjectFields) { i = (int) dObjectFields["i"]; list = (int) dObjectFields["list"]; ss = (SampleSubclass) dObjectFields["ss"]; } public void WriteConfig(XMLConfigWriter writer) { writer.WriteObject("i", i); writer.WriteList("list", list); writer.WriteObject("ss", ss); } }
And here's how you serialize and deserialize this object:
SampleClass sc = new SampleClass(); XMLConfigWriter.Write(strFilename, sc); // writes to file sc = (SampleClass) XMLConfigReader.Read(strFilename); // reads from fileAdditional features of XMLConfig:
writer.WriteObject("fieldName", writer.EncryptString(strPlainText, "MyPassword"));
strPlainText = writer.DecryptString((string) dObjectFields["fieldName"], "MyPassword");