c# - How would I serialize XML with nested Elements into an object -
i have xml has nested tags following:
<?xml version="1.0" encoding="utf-8" ?> <questions> <question title="which of these circle?" type="graphics"> <text>which of these circle?</text> <image src="shapes.png" /> <answer correct="true"> <image src="circle.png"/> </answer> <answer> <image src="square.png"/> </answer> </question> <question title="click on circle" type="point"> <image src="imageofshapeswiththeanswerat200x150withasizeof20x20.png"/> <answer x="200" y="150" width="20" height="20" correct="true">circle</answer> <answer x="100" y="150" width="20" height="20">notcircle</answer> </question> <question title="trick question" type="text"> <text>what colour of duke wellingtons white horse?</text> <image src="images.png" /> <answer correct="true">white</answer> <answer>blue</answer> <answer>black</answer> <answer>red</answer> <answer>green</answer> </question> </questions>
how serialize class?
[xmlroot("quiz")] public class quiz { public class question { public string questiontext { get; set; } public string questiontitle { get; set; } // automatic getters , setters (nicer) public string questionimage { get; set; } [xmlarray] public list<answer> answers { get; set; } } public class answer { public boolean selected { get; set; } public boolean correct { get; set; } [xmlelement("text")] public string text { get; set; } [xmlelement("image")] public string image { get; set; } } }
i've tried using serializer hence [xmlelement]
, [xmlroot]
in object i'm struggling bit it.
i've seen lot of examples of serializing xml not many xml object.
edit
i've found tutorial http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
xdocument dok = xdocument.load(server.mappath("xmlfile.xml")); xmlserializer myserializer = new xmlserializer(typeof(quiz)); textreader tw = new stringreader(dok.tostring()); quiz quizdata= myserializer.deserialize(tw) quiz;
deserialize method of xmlserializer can that.
Comments
Post a Comment