开发者

Error serializing linq result

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\开发者_StackOverflowAbcom.Payroll.Register\MainWindow.xaml.vb:line 415**

Does anybody know what is going on here and how can I solve this problem?

Regards,

Claudio


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜