-1

I have class

public class IPTable : SerializableDictionary<string, string>
{
...
}

trying to deserialize IPTable:

XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, string>));
StreamReader textReader = new StreamReader(xmlFileName);
SerializableDictionary<string, string> ip =( SerializableDictionary < string, string>) serializer.Deserialize(textReader);
return (IPTable)ip;

Got exception below while casting SerializableDictionary<string, string> to IPTable.

Message = "Unable to cast object of type 'SerializableDictionary`2[System.String,System.String]' to type 'IPTable'."

How to deserialize to IPTable?

vico
  • 17,051
  • 45
  • 159
  • 315
  • You're telling your serializer to serialize it as a `SerializableDictionary));. –  Mar 29 '17 at 11:04
  • You cannot serialize a dictionary (see : http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member) You can manually parse the xml using either XDocument or XmlDocument. – jdweng Mar 29 '17 at 11:06
  • @jdweng You can serialize a `SerializableDictionary` though https://msdn.microsoft.com/en-us/library/gg496181.aspx The fact that the exception message says "Unable to cast object of type 'SerializableDictionary2[System.String,System.String]' to type 'IPTable'." means it's already successfully deserialized the `SerializableDictionary`. –  Mar 29 '17 at 11:08
  • Andy : Count it mean that the dictionary should be defined as : SerializableDictionary> – jdweng Mar 29 '17 at 11:11
  • "Unable to cast object of type `SerializableDictionary2[System.String,System.String]` to type `IPTable`." means "I have an object of the type `SerializableDictionary2[System.String,System.String]`, but I can't cast it in to an object of type `IPTable`". –  Mar 29 '17 at 11:15

1 Answers1

3

Create your XmlSerializer with IPTable

XmlSerializer serializer = new XmlSerializer(typeof(IPTable));
StreamReader textReader = new StreamReader(xmlFileName);
return (IPTable)serializer.Deserialize(textReader);
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57