Because I haven't found a better way to handle my configuration, I have been using XmlSerializer to serialize a Dictionary (explain that later) containing all the things I want saved. Now that I have gotten more crafty, I have started to build more complex data types. The only problem is that now when I serialize the object, it throws an error because its not a primitive data type. If I make a new serializer for every value, I then cannot load the object back. Is there an easy way around this? Or an easy library for saving and loading any data type.
The Serializable Dictionary. I'm using <string,object>
for <T,S>
public void ReadXml (System.Xml.XmlReader reader)
{
try {
XmlSerializer deserializerKeys = new XmlSerializer( typeof(T) );
XmlSerializer deserializerValues = new XmlSerializer( typeof(S) );
reader.Read();
while (true) {
T key = (T)deserializerKeys.Deserialize(reader);
S val = (S)deserializerValues.Deserialize(reader); // fail :(
this.Add( key, val );
}
} catch ( Exception e ) {
Console.WriteLine( e.Message );
Console.WriteLine( e.StackTrace );
}
}
public void WriteXml (System.Xml.XmlWriter writer)
{
// ONE OF THE serializerValues is always commented out
// Im just showing the two ways I have done this
XmlSerializer serializerKeys = new XmlSerializer( typeof( T ) );
// this works for loading but not for saving >.>
XmlSerializer serializerValues = new XmlSerializer( typeof( S ) );
foreach ( KeyValuePair<T,S> pair in this ) {
// this way works with the saving but fails with loading
XmlSerializer serializerValues = new XmlSerializer(pair.Value.GetType());
serializerKeys.Serialize( writer, pair.Key );
serializerValues.Serialize( writer, pair.Value );
}
}
Then I have another class that is derived from List<SimpleRegex>
, it holds all the SimpleRegex
objects and a "selected regex". The SimpleRegex
is just a class that holds a simpler regex syntax and provides the same sort of methods a normal regex would(match, replace, etc.). That way, normal people can use it. This RegexCollection
(the List<SimpleRegex>
) is not primitive. That is my problem. Basically, all I want to do is save and load any data type.