我希望能够在我的对象结构中反映出XML树,但我对LINQ to XML还是个初学者。
我有一个以下结构的XML:
当然,这种方式可以让我每次都将所有问题的答案添加到每个列表中。如何解决?我可以想象这很简单,但我不知道怎么做 :)
提前感谢您!
我有一个以下结构的XML:
<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>
我有以下类:
public class Question
{
public string text { get; set; }
public IList<Anwser> anwsers = new List<Anwser>();
}
public class Anwser
{
public string content { get; set; }
}
我曾经构建了下面这个错误的Linq查询:
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();
当然,这种方式可以让我每次都将所有问题的答案添加到每个列表中。如何解决?我可以想象这很简单,但我不知道怎么做 :)
提前感谢您!
LINQ
解析 XML 是否比使用XmlDocument
有任何优势? - B Faley