vb.net - XML Serializing objects that contain other objects (recursive serialization) -
i have class called port characteristics
follows:
<serializable()> public class portcharacteristics public portnum int16 public brate integer public par string public len string public stopb string public flow string public sub new() end sub public sub new(byval valportnum int16, byval valbrate integer, byval valpar string, byval vallen string, byval valstopb string, byval valflow string) portnum = valportnum brate = valbrate par = valpar len = vallen stopb = valstopb flow = valflow end sub end class
i have class, profile
, contains port characteristics
:
<serializable()> public class profile public profilename string public chars portcharacteristics public property portchar portcharacteristics return chars end set(value portcharacteristics) chars = value end set end property public sub new() end sub public sub new(byval profname string, byval portnum int16, byval baud integer, byval par string, byval length string, byval stopb string, byval flowcon string) name = profname portchar = new portcharacteristics(portnum, baud, par, length, stopb, flowcon) end sub end class
i serialize profile class this:
if my.computer.filesystem.fileexists("profiles.xml") my.computer.filesystem.deletefile("profiles.xml") end if using fs new filestream("profiles.xml", filemode.openorcreate) dim xml new xmlserializer(gettype(list(of profile))) xml.serialize(fs, profilelist) end using
is possible serialize objects within objects in xml in same file, or have use binary or other type of serialization?
first, there no recursion involved, "simply" have 1 object nested in another. second, profile
class code wrong - references members not exist; none of listed on portcharstring
exist on other class.
i cannot tell role profile
plays. shown, wrapper adds name portcharacteristics
item. added other class. if profile
meant assign name , store several items, lacks sort of collection object. there other issues, suspect problem here:
dim xml new xmlserializer(gettype(list(of profile))) xml.serialize(fs, profilelist)
we can't tell posted if profilelist
type or object variable. works (i changed names):
dim plist new list(of portprofile) plist.add(new portprofile("com9:", 9, 1200, "e", "7", "1", "x")) plist.add(new portprofile("com13:", 13, 300, "e", "8", "1", "x")) using fs new filestream(portprofilename, filemode.openorcreate) dim xml new xmlserializer(plist.gettype) xml.serialize(fs, plist) end using
make sure type
used in xmlserializer
constructor matches object variable being used, , work fine.
Comments
Post a Comment