我有一个父表类和一个子表主题。我已经创建了这些类的对象,并将子对象添加为父类中的列表。现在我想使用 linq 读取父表数据并使用 xml 序列化将其转换为 xml 文件。
这是我的代码
ClassMaster cls = new ClassMaster();List<ClassMaster> clsList =
cls.FindAll().Where(t => t.ClassSymbol == "I").ToList();
var serializer1 = new XmlSerializer(cls.FindAll().GetType());
ClassMaster cls = new ClassMaster();
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(cls.GetType());
serializer.Serialize(stringwriter, cls);
但它在第 3 行抛出异常
“无法序列化成员 'System.Collections.Generic.IList`1 [[School.Objects.ClassWiseSubject, School.Objects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 类型的成员 'School.Objects.ClassMaster.classSubjectList' ]]'"
public class ClassMaster : GenericRepository<ClassMaster>
{
public virtual int ClassId { get; set; }
public virtual string ClassSymbol { get; set; }
public virtual string ClassName { get; set; }
public virtual IList<ClassWiseSubject> classSubjectList { get; set; }
}
public class ClassWiseSubject : GenericRepository<ClassWiseSubject>
{
public virtual int Id { get; set; }
public virtual int ParentID { get; set; }
public virtual int SerialNo { get; set; }
public virtual string SubjectCode { get; set; }
}
HBM 配置文件:
<class name="ClassMaster" table="tbl_ClassMaster">
<id name="ClassId" column="ClassId" type="int">
<generator class="identity"></generator>
</id>
<property name="ClassSymbol" column="ClassSymbol" type="string"/>
<property name="ClassName" column="ClassName" type="string"/>
<list name="classSubjectList" cascade="all" lazy ="false">
<key column="ParentID"/>
<index column="SerialNo"/>
<one-to-many class="ClassWiseSubject"/>
</list>
</class>
<class name="SubjectMaster" table="tbl_SubjectMaster">
<id name="SubjectId" column="SubjectId" type="int">
<generator class="identity"></generator>
</id>
<property name="SubjectCode" column="SubjectCode" type="string"/>
<property name="SubjectName" column="SubjectName" type="string"/>
</class>
谢谢苏拉吉特