.net - how to convert parent object with child object list into xml file using c#? -
i have parent table class , child table subject. have created objects of these classes , child objects added list in parent class. want read parent table data using linq , convert xml file using xml serialization.
here's code
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);
but throwing exception in line 3
"cannot serialize member 'school.objects.classmaster.classsubjectlist' of type '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 config file :
<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>
thanks surajit
xmlserializer
doesn't support ilist<t>
interface. have several options:
- change type
ilist<t>
concrete class (e.g.list<t>
); - use
datacontractserializer
instead ofxmlserializer
; - implement
ixmlserializable
; - create fake property of type
list<t>
xmlserializer
reading , writingclasssubjectlist
, markclasssubjectlist
[xmlignore]
attribute.
Comments
Post a Comment