I have an xml that has nested tags like the following:
<?xml version="1.0" encoding="utf-8" ?>
<questions>
<question title="Which of these is a circle?" type="graphics">
<text>Which of these is a 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 the 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 was the 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 would I then serialize it into this class?
[XmlRoot("quiz")]
public class Quiz
{
public class Question
{
public String QuestionText { get; set; }
public String QuestionTitle { get; set; } // automatic getters and 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 the serializer hence the [XmlElement] and [XmlRoot] in the object but i’m struggling a bit with it.
I’ve seen a lot of examples of serializing into xml but not many from xml to an object.
edit
i’ve found a tutorial for this http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
Deserialize method of the XmlSerializer can do that.