Hi,
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!