在linq中返回多个具有相同名称的xml子项

时间:2012-06-18 16:59:22

标签: c# xml linq

我有以下XML并希望将所有“Answers”子项作为List

返回
<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;

    }

3 个答案:

答案 0 :(得分:1)

将班级更改为

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()

                        };

答案 1 :(得分: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);
}

答案 2 :(得分:0)

尝试更改此内容:

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

到此:

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