c# - Poplulating an serializing a Dictionary -
was hoping inquire help in serializing dictionary xml file.
i extract info database , appears follow , want in xml file.
this class(object) intend serialize along method serialize list as.
public class product { static public void serializetoxmlcollection(list<product> products) { xmlserializer serializer = new xmlserializer(typeof(list<product>)); string path = string.concat(environment.currentdirectory, "../../products.xml"); textwriter textwriter = new streamwriter(path); serializer.serialize(textwriter, products); textwriter.close(); } public guid guid { get; set; } public dictionary<string, list<string>> mydictionary { get; set; }
this query along list utilize add together each product instance
list<product> products = new list<product>(); while (dbread.read()) { product p = new product(); string code = (string)dbread["prodid"]; string subject = (string)dbread["subject"]; string generalsubject = (string)dbread["generalsubject"]; p.guid = guid.newguid(); if (dict.containskey(code)) { dict[code].add(subject); } else { dict.add(code, new list<string> { subject, generalsubject}); } //i assign dict object mydictionary in product class p.mydictionary = dict; //now add together entire object list<product> products serialize list products.add(p);
finally seek serialize list
product.serializetoxmlcollection(products);
but invalidoperationexception.
how can serialize object? think problem object contains dictionary.
i believe datacontractserializer supports dictionary objects. remember using @ point, can't seem find of own examples.
there examples on official docs page here: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx
otherwise, may need utilize replacement dictionary class supports xml serialization, such http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
c# dictionary xml-serialization
No comments:
Post a Comment