WCF does not know that you have a List<string>
there - note that all other <Value>
elements have a "hint type" (attribute i: type). If you want to deserialize it, it must be labeled, and you also need to tell WCF that the List<string>
is a "known type" - see below. For more information about known types (and why they are needed), there are many resources on the web .
public class StackOverflow_7620718 { public static void Test() { Dictionary<string, object> dict = new Dictionary<string, object> { { "name", "Test object" }, { "x", 0.5 }, { "y", 1.25 }, { "age", 4 }, { "list-of-strings", new List<string> { "one string", "two string", "last string" } } }; MemoryStream ms = new MemoryStream(); XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false), IndentChars = " ", OmitXmlDeclaration = true, }); DataContractSerializer dcs = new DataContractSerializer(dict.GetType(), new Type[] { typeof(List<string>) }); dcs.WriteObject(w, dict); w.Flush(); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); ms.Position = 0; Console.WriteLine("Now deserializing it:"); Dictionary<string, object> dict2 = (Dictionary<string, object>)dcs.ReadObject(ms); foreach (var key in dict2.Keys) { Console.WriteLine("{0}: {1}", key, dict2[key].GetType().Name); } } }
source share