序列化linq结果时出错

时间:2010-12-02 23:45:21

标签: xml vb.net linq

我正在尝试以这种方式序列化LINQ结果:

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  

但是在行中:

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

我收到了这个错误:

**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**

有人知道这里发生了什么,我该如何解决这个问题?

的问候,
克劳迪奥

1 个答案:

答案 0 :(得分:2)

奇怪的是,你要序列化那个。 但是如果您愿意:只需将.ToList()添加到您的sourceForXML,它就会序列化。但结果会很奇怪。 (不知道VB中是否可以使用方法扩展,如果没有,则使用Enumerable.ToList( /* From .... Join ... here */)
Imho,很好的解决方案是不使用anon类型进行序列化。创建具有需要保存的属性的类,然后使用linq创建该项的列表,并在序列化该集合之后创建。在C#中,它类似于:

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

您的方法应该为该类的实例提取所需的信息:

var xmlData = PayrollRegisterModel.CompanyDetails
       .Join(/* other table */) 
       .Select(x => new DataForXml { Field1 = x.Field1, Field2 = x.Field2 /* init other props here*/})
       .ToList();