Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to parse XML tree with Linq to XML (C#)

I would like to reflect the XML tree in my object structure, but I am quite a beginner in LINQ to XML

I have an XML with following structure:

<questions>
<question id="q1">
  <number>1</number>
  <text>some text11</text>
  <answers>
     <answer>
      <v>some text11</v>
    </answer>
    <answer>
      <v>some text11</v>
    </answer>
  </answers>
</question>
<question id="q2">
  <number>2</number>
  <text>some text2</text>

<answers>
    <answer>
      <v>some text22</v>
    </answer>
    <answer>
      <v>some text22</v>
    </answer>
  </answers>
</question>
<question id="q3">
  <number>3</number>
  <text>some text3</text>
  <answers>
    <answer>
      <v>some text33</v>
    </answer>
    <answer>
      <v>some text33</v>
    </answer>
    <answer>
      <v>some text33</v>
      <addDescription>some text333</addDescription>
      <textBox/>
    </answer>
  </answers>
</question>
</questions>

...and I have following classes:

public class Question
{
    public string text { get; set; }
    public IList<Anwser> anwsers = new List<Anwser>();
}

public class Anwser
{
    public string content { get; set; }
}

... and I have build a following (wrong) Linq query:

        List<Question> questions = (from xml in xdoc.Element("survey").Elements("questions").Elements("question")
                                    select new Question()
                                               {
                                                   text = xml.Element("text").Value,
                                                   anwsers =
                                                       (from anwsers in
                                                            xdoc.Element("survey").Elements("questions").Elements("question").Elements(
                                                            "answers").Elements(
                                                            "answer")
                                                        select new Anwser()
                                                                   {
                                                                       content = anwsers.Element("v").Value
                                                                   }

                                                       ).ToList()
                                            }).ToList();

Of course this way I get each time all anwsers from all questions added to each list. How to solve this? I can imagine that this is simple but I have no idea :)

Thank you in advance!

like image 684
Kamil Zadora Avatar asked Apr 26 '09 22:04

Kamil Zadora


1 Answers

Your code isn't working because you are getting back all the answer elements because you didn't restrict them based on the question they came from. You could add this restriction or instead of a subquery based on the document, you can make the subquery based on the question element itself.

List<Question> questions = (from question in xdoc.Element("survey").Element("questions").Elements("question")
         select new Question
         {
           text = question.Element("text").Value,
           anwsers = (from answer in question.Element("answers").Elements("answer")
                select new Anwser
                {
                  content = answer.Element("v").Value
                }).ToList()
         }).ToList();
like image 155
Samuel Avatar answered Nov 02 '22 06:11

Samuel