c# - How to create incremented XML element names when serializing a list -
i'm attempting serialize level object looks this:
public class level { //some xmlatributes [xmlelement] public list<question> questions; } public class question { //some xmlattributes [xmlarray("answers")] public list<string> answers; }
into , xml file looks this:
<level time="2"> <question type="multiplechoice"> <answers correct="b"> <a>answer 1</a> <b>answer 2</b> <c>answer 3</c> <d>answer 4</d> </answers> </question> <question> ... </question> </level>
i can serialize except element names under <answers>
. notice how each element gets incremented name, rather beingness same (e.g. <string>
default). possible? know can rename elements [xmlarrayitem("itemname")]
, applies same name elements in array.
if else happens across this, here solution went (thanks iabstract's tip):
public class reply : ixmlserializable { public list<string> list = new list<string>(); public string this[int pos] { { homecoming list[pos]; } set { list[pos] = value; } } public void readxml ( xmlreader reader ) { reader.readtodescendant("a"); while ( reader.name != "answers" ) { if ( reader.isstartelement() ) { list.add(reader.readelementcontentasstring()); } } reader.read(); } public void writexml ( xmlwriter author ) { ( int = 0; < list.count; i++ ) { writer.writeelementstring(((char)(97 + i)).tostring(), list[i]); } } public xmlschema getschema() { return(null); } }
i made reply it's own class has attributes , don't need override serialization behavior else within question class. reader.read() create sure reader moved next tag after , or else deserialization won't go on after dealing answer.
it's not 100% perfect, implementation it's enough.
c# xml-serialization
No comments:
Post a Comment