我有以下 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>
<mchoice>
<question>Launceston is the second largest city in which Australian state?</question>
<answer>Victoria</answer>
<answer>New South Wales</answer>
<answer correct="yes">Tasmania</answer>
<answer>Western Australia</answer>
</mchoice>
</quiz>
public class Question
{
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; }
}
我尝试了以下 linq 查询,但我被困在答案字段中
public IEnumerable<Question> GetAll()
{
var questions = from docs in _doc.Descendants("mchoice")
select new
{
QuestionText = docs.Element("question").Value,
Answers = docs.Descendants("answer").SelectMany(e=>e.Element("answer").Value)
};
return questions;
}