我有一個父表類和一個子表主題。我創建了這些類的對象和子對象作爲父類中的列表添加。現在我想使用linq讀取父表數據,並使用xml序列化將其轉換爲xml文件。如何將具有子對象列表的父對象轉換爲使用c#的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線
拋出一個異常「無法序列成員 'School.Objects.ClassMaster.classSubjectList' 類型的 「System.Collections中。 Generic.IList`1 [[School.Objects.ClassWiseSubject, School.Objects,Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null]]''
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>
感謝 Surajit
[編輯]並修復您的格式。不要試圖將代碼放在編號列表中,這是行不通的。 – Will