c# - Format XML containing List for Deserialization -
i have data need bring xml file postgresql database through c# application working on.
the problem face need add kind of list xml now. looks this:
<itemone>stuff</itemone> <itemtwo>otherstuff</itemtwo> <listofitems> <listitem> <partone>123</partone> <parttwo>abc</parttwo> </listitem> <listitem> <partone>123</partone> <parttwo>abc</parttwo> </listitem> </listofitems>
so found quite threads handle topic of how deserialize lists. of them start saying proposed xml should modified. have freedom wish in regard, i'd first create optimal xml format @ first.
well, , afterwards deserialize it. if there format lists automatically processed xmlserializer.deserialize() amazing.
that data processed automatically xmlserializer
, long class shaped match. example:
[xmlroot("yourrootname")] public class foo { [xmlelement("itemone")] public string {get;set;} [xmlelement("itemtwo")] public string b {get;set;} [xmlarray("listofitems")] [xmlarrayitem("listitem")] public list<bar> bars {get;set;} } public class bar { [xmlelement("partone")] public int c {get;set;} [xmlelement("parttwo")] public string d {get;set;} }
of course, if me i'd tempted terse, with:
<bettername foo="stuff" blab="otherstuff"> <nameme a="123" b="abc"/> <nameme a="456" b="def"/> </bettername>
which requires minimal changes:
[xmlroot("bettername")] public class foo { [xmlattribute("foo")] public string {get;set;} [xmlattribute("bar")] public string b {get;set;} [xmlelement("nameme")] public list<bar> bars {get;set;} } public class bar { [xmlattribute("a")] public int c {get;set;} [xmlattribute("b")] public string d {get;set;} }
Comments
Post a Comment