我有以下xml结构
<quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="quiz.xsd">
<mchoice>
<question>What is the capital city of Australia?</question>
<answer>Sydney</answer>
<answer correct="yes">Canberra</answer>
<answer>Melbourne</answer>
<answer>Gold Coast</answer>
</mchoice>
</quiz>
我想使用以下代码将其转换为 CLR 类型
public class Question
{
public int ID { get; set; }
public string QuestionText { get; set; }
public List<Answer> Answers { get; set; }
}
public class Answer
{
public string Answer1 { get; set; }
public string Answer2 { get; set; }
public string Answer3 { get; set; }
public string Answer4 { get; set; }
}
public List<Question> GetAll()
{
var doc = XDocument.Load(Filepath);
var results = (from x in doc.Descendants("mchoice")
select new Question()
{
ID = Convert.ToInt16(x.Element("ID")),
QuestionText = x.Element("question").Value.ToString(),
Answers = new List<Answer>()
{
//How I would fill Answer collection
}
}).ToList();
return results;
}
现在我对当前的设计有两个问题。
- 我将如何填写答案集合
- 我将如何处理当前设计中的正确答案属性。