0

I'm trying to serialize a LINQ result in this way:

Private Sub btnXML_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnXML.Click  
    Try  
        Dim sourceForXML = From detail In PayrollRegisterModel.CompanyDetails  
                           Join shifts In PayrollRegisterModel.Shifts On   detail.Id_companydetail Equals shifts.Id_companydetail  
        Dim xmlFile As New Xml.Serialization.XmlSerializer(sourceForXML.GetType)  
        Dim xw As System.Xml.XmlWriter = Xml.XmlWriter.Create("C:/Abcom/XMLRegister.xml")  
        xmlFile.Serialize(xw, sourceForXML)  
    Catch ex As Exception  
        MsgBox(e.ToString)  
    End Try  
End Sub  

but in the line:

        Dim xmlFile As New Xml.Serialization.XmlSerializer(sourceForXML.GetType) 

I'm getting this error:

**System.InvalidOperationException was caught
  Message=To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their 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]] does not implement Add(System.Object).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.TypeScope.GetEnumeratorElementType(Type type, TypeFlags& flags)
       at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
       at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
       at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
       at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type)
       at 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 anybody know what is going on here and how can I solve this problem?

Regards,
Claudio

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Claudio
  • 1,987
  • 3
  • 29
  • 55

1 Answers1

2

It's odd, that you want to serialize that. But if you wish: Just add .ToList() to your sourceForXML and it will serialize. But the result will be strange. (Don't know if method extensions are availiable in VB, if not, then use Enumerable.ToList( /* From .... Join ... here */))
Imho, good solution is not to use anon types for serialization. Create class with properties you need to save, then create list of that items using linq, and after serialize that collection. In C# it is something like:

class DataForXml
{
    public string Field1 {get; set;}
    public string Filed2 {get; set;}
    // other needed fields here
}

And your method should extract needed info to instances of that class:

var xmlData = PayrollRegisterModel.CompanyDetails
       .Join(/* other table */) 
       .Select(x => new DataForXml { Field1 = x.Field1, Field2 = x.Field2 /* init other props here*/})
       .ToList();
The Smallest
  • 5,713
  • 25
  • 38
  • Actualy I dont know exactly how to translate this code from C# to VB. Anyway, my solution will be change my language! Thanks a lot! – Claudio Dec 03 '10 at 00:41
  • @Claudio Ooops ^_^ Didn't mean to make you choose another language) Good luck in C#, it's powerful and amazing, IMHO. You would like it (sooner or later))) – The Smallest Dec 03 '10 at 00:45