santosh singh
santosh singh

Reputation: 28692

Returning multiple xml Children with same name in linq

I have the following XML and want to return all "Answers" children as 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; }
}

I tried the following linq query,but I am stuck in Answer field

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;

    }

Upvotes: 1

Views: 1089

Answers (3)

Shyju
Shyju

Reputation: 218942

I am not quite sure Logically your entity structure is correct. You seems to have a Answers property of type List and each answer entity have again 4 (always 4 ? ) answers again !

I guess, changing it like this would make more sense.

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; }
}

And you can write a method which returns the List of Questions (with answers) by reading your 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;
} 

And I can always call it like this wherever i want

XElement elm = XElement.Load(Server.MapPath(@"../YourFolder/sample.xml"));
//You can alternatively load from string/stream etc..
if (elm != null) 
{
  var questionList=GetAll(elm);
}

Upvotes: 1

L.B
L.B

Reputation: 116188

Change your class as

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

}

Then your query will be

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

                        };

Upvotes: 2

m.edmondson
m.edmondson

Reputation: 30922

Try changing this:

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

to this:

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

Upvotes: 0

Related Questions