serialization - XmlSerializer: The type of the argument object is not primitive -


system.invalidoperationexception: type of argument object 'si_foodware.model.localisationcollection' not primitive. system.invalidoperationexception: there error generating xml document.

localisationcollection.cs

using system.xml.serialization;  namespace si_foodware.model {     [xmlroot("localisationcollection")]     public class localisationcollection     {         [xmlarray("localisationitems")]         [xmlarrayitem("localisationitem", typeof(localisationitem))]         public localisationitem[] localisationitem { get; set; }     } } 

localisationitem.cs

using system.xml.serialization; using sqlite.net.attributes;  namespace si_foodware.model {     public class localisationitem     {         [primarykey, autoincrement]         [xmlignore]         public int id { get; set; }          [xmlelement("page")]         public string page { get; set; }          [xmlelement("field")]         public string field { get; set; }          [xmlelement("language")]         public string language { get; set; }          [xmlelement("value")]         public string value { get; set; }          [xmlelement("width")]         public string width { get; set; }          [xmlelement("columns")]         public string columns { get; set; }          [xmlelement("table")]         public string table { get; set; }          [xmlelement("title")]         public string title { get; set; }          [xmlelement("parent")]         public string parent { get; set; }          [xmlelement("iconsource")]         public string iconsource { get; set; }          [xmlelement("targettype")]         public string targettype { get; set; }    } } 

function serialize

    public bool serialize(string filename)     {         var path = getpath(filename);         var serializer = new xmlserializer(typeof(list<localisationcollection>));         var writer = new filestream(path, filemode.create);         var localisationitems = db.getallitems<localisationitem>();         var collection = new localisationcollection();          collection.localisationitem = localisationitems.toarray();          try         {             serializer.serialize(writer, collection);             writer.close();             return true;         }         catch (exception ex)         {             throw new exception(ex.message);         }     } 

i want somethink this

<?xml version="1.0" encoding="utf-8"?> <localisationcollection>     <localisationitems>         <localisationitem>             <language>nederlands</language>         </localisationitem>         <localisationitem>             <language>engels</language>         </localisationitem>         <localisationitem>             <page>loginpage</page>             <field>grd_grid</field>             <columns>2</columns>         </localisationitem>         <localisationitem>             <page>loginpage</page>             <field>grd_grid</field>             <width>120</width>         </localisationitem>         <localisationitem>             <page>loginpage</page>             <field>grd_grid</field>             <width>180</width>         </localisationitem>     </localisationitems> </localisationcollection> 

you trying serialize localisationcollection xmlserializer constructed list<localisationcollection>:

   var serializer = new xmlserializer(typeof(list<localisationcollection>));    var collection = new localisationcollection();    serializer.serialize(writer, collection); 

this not work. must use serializer constructed same type being serialized:

    var serializer = new xmlserializer(typeof(localisationcollection)); 

to avoid mistake, create following static helper method:

public static class xmlserializationhelper {     public static void serializetofile<t>(this t obj, string path, xmlwritersettings settings = null, xmlserializer serializer = null)     {         if (obj == null)             throw new argumentnullexception("obj");         using (var stream = new filestream(path, filemode.create))         using (var writer = xmlwriter.create(stream, settings))         {             serializer = serializer ?? new xmlserializer(obj.gettype());             serializer.serialize(writer, obj);         }     } } 

then serialize method becomes:

    public bool serialize(string filename)     {         var path = getpath(filename);         var localisationitems = db.getallitems<localisationitem>();         var collection = new localisationcollection { localisationitem = localisationitems.toarray() };          collection.serializetofile(path);          return true;     } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -