xml - Error serializing linq result -


i'm trying serialize linq result in way:

private sub btnxml_click(byval sender object, byval e system.windows.routedeventargs) handles btnxml.click       try           dim sourceforxml = detail in payrollregistermodel.companydetails                              join shifts in payrollregistermodel.shifts on   detail.id_companydetail equals shifts.id_companydetail           dim xmlfile new xml.serialization.xmlserializer(sourceforxml.gettype)           dim xw system.xml.xmlwriter = xml.xmlwriter.create("c:/abcom/xmlregister.xml")           xmlfile.serialize(xw, sourceforxml)       catch ex exception           msgbox(e.tostring)       end try   end sub   

but in line:

        dim xmlfile new xml.serialization.xmlserializer(sourceforxml.gettype)  

i'm getting error:

**system.invalidoperationexception caught   message=to xml serializable, types inherit ienumerable must have implementation of add(system.object) @ levels of inheritance hierarchy. system.data.objects.objectquery`1[[vb$anonymoustype_0`2[[abcom.payroll.register.companydetail, abcom.payroll.register, version=1.0.0.0, culture=neutral, publickeytoken=null],[abcom.payroll.register.shift, abcom.payroll.register, version=1.0.0.0, culture=neutral, publickeytoken=null]], abcom.payroll.register, version=1.0.0.0, culture=neutral, publickeytoken=null]] not implement add(system.object).   source=system.xml   stacktrace:        @ system.xml.serialization.typescope.getenumeratorelementtype(type type, typeflags& flags)        @ system.xml.serialization.typescope.importtypedesc(type type, memberinfo memberinfo, boolean directreference)        @ system.xml.serialization.typescope.gettypedesc(type type, memberinfo source, boolean directreference, boolean throwonerror)        @ system.xml.serialization.modelscope.gettypemodel(type type, boolean directreference)        @ system.xml.serialization.xmlreflectionimporter.importtypemapping(type type, xmlrootattribute root, string defaultnamespace)        @ system.xml.serialization.xmlserializer..ctor(type type, string defaultnamespace)        @ system.xml.serialization.xmlserializer..ctor(type type)        @ abcom.payroll.register.mainwindow.btnxml_click(object sender, routedeventargs e) in c:\abcom\inhouse development tools\abcom.payroll.register\abcom.payroll.register\mainwindow.xaml.vb:line 415** 

does know going on here , how can solve problem?

regards,
claudio

it's odd, want serialize that. if wish: add .tolist() sourceforxml , serialize. result strange. (don't know if method extensions availiable in vb, if not, use enumerable.tolist( /* .... join ... here */))
imho, solution not use anon types serialization. create class properties need save, create list of items using linq, , after serialize collection. in c# like:

class dataforxml {     public string field1 {get; set;}     public string filed2 {get; set;}     // other needed fields here } 

and method should extract needed info instances of class:

var xmldata = payrollregistermodel.companydetails        .join(/* other table */)         .select(x => new dataforxml { field1 = x.field1, field2 = x.field2 /* init other props here*/})        .tolist(); 

Comments