1

我有以下 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;

    }
4

3 回答 3

2

将您的课程更改为

public class Question
{
    public string QuestionText { get; set; }
    public List<string> Answers { get; set; }

}

那么您的查询将是

var questions = from docs in _doc.Descendants("mchoice")
                        select new Question
                        {
                            QuestionText = docs.Element("question").Value,
                            Answers = docs.Elements("answer").Select(a=>a.Value).ToList()

                        };
于 2012-06-18T17:22:51.317 回答
1

我不太确定逻辑上您的实体结构是否正确。您似乎有一个 List 类型的 Answers 属性,每个答案实体又有 4 个(总是 4 个?)答案!

我想,像这样改变它会更有意义。

public class Question
{
    public string QuestionText { get; set; }
    public List<Answer> Answers { get; set; }

    public Question()
    {
        if (Answers == null)
            Answers = new List<Answer>();
    }

}
public class Answer
{
    public string Answer1 { get; set; }
}

您可以通过阅读您的 XML 来编写一个返回问题列表(带有答案)的方法

public static IEnumerable<Question> GetAll(XElement elm)
{
    var allQA = new List<Question>();
    var mchoices = elm.Descendants("mchoice").ToList();
    foreach (var choice in mchoices)
    {
        var answers = choice.Descendants("answer").ToList();
        var qA = new Question { QuestionText = choice.Descendants("question").SingleOrDefault().Value };
        foreach (var answer in answers)
        {
            qA.Answers.Add(new Answer { Answer1 = answer.Value});
        }
        allQA.Add(qA);               
    }
    return allQA;
} 

我可以随时随地这样称呼它

XElement elm = XElement.Load(Server.MapPath(@"../YourFolder/sample.xml"));
//You can alternatively load from string/stream etc..
if (elm != null) 
{
  var questionList=GetAll(elm);
}
于 2012-06-18T17:30:49.237 回答
0

尝试改变这个:

Answers1 = docs.Descendants("answer").SelectMany(e=>e.Element("answer").Value)

对此:

Answers1 = docs.Descendants("answer").SelectMany(e=>e.Value)

于 2012-06-18T17:04:48.053 回答