Declaring the datatype dynamically reading from an xml string
I've this strange issue. I've an xml string which looks like below -
<key><int>5</int></key><value><int>10</int>
The above xml is obtained after serializing a Dictionary using Paul's Code. Now i want to convert the xml back to the dictionary.
How can i get the type "int" from th开发者_运维百科e xml and declare the dictionary as follows?
Dictionary<int, int>
Any clues?
Old question, I know, but here is a generic approach that works with a serialized SerializableDictionary (by one Paul, as you pointed out). If this is still pertinent, can you try it?
SerializableDictionary<int, int> stuffDict = ReadXML<int, int>(@"c:\test.xml");
private static SerializableDictionary<T, U> ReadXML<T, U>(string file)
{
SerializableDictionary<T, U> dict = new SerializableDictionary<T, U>();
if (File.Exists(file))
{
FileStream flStream = new FileStream(file, FileMode.Open, FileAccess.Read);
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SerializableDictionary<T, U>));
dict = xmlSerializer.Deserialize(flStream) as SerializableDictionary<T, U>;
}
finally
{
flStream.Close();
}
}
return dict;
}
精彩评论