I'm having trouble taking data from an XML file and reading it into my program. Here's the code I'm trying to use:
_xmlPath = NSBundle.MainBundle.PathForResource ("Questions", "xml");
using (var reader = new StreamReader (_xmlPath))
{
var serializer = new XmlSerializer (typeof(Questions));
var questionData = (Questions)serializer.Deserialize (reader);
}
The error I receive is at the line: var questionData = (Questions)serializer.Deserialize (reader);
and the error I receive is System.InvalidOperationException was thrown. There is an error in XML document.
I'm trying to populate a Questions class which is as follows:
[Preserve(AllMembers = true)]
public class Questions
{
public string QuestionTitle {get;set;}
public List<string> Answers { get; set; }
}
Here is a snippet example of my XML file:
<?xml version="1.0" encoding="utf-8" ?>
<topic>
<question>Question 1</question>
<answers>
<response>Answer 1</response>
<response>Answer 2</response>
<response>Answer 3</response>
<response>Answer 4</response>
</answers>
</topic>
<topic>
<question>Question 2</question>
<answers>
<response>Answer 1</response>
<response>Answer 2</response>
</answers>
</topic>
What I'm trying to do is read the file into a List where each topic in the XML would represent an index in the List. For example:
questionList[0].question -> would return "Question 1"
questionList[1].answers[1].response -> would return "Answer 2"
etc.