Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试用Linq解析XML树到XML(C)_C#_.net_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 尝试用Linq解析XML树到XML(C)

C# 尝试用Linq解析XML树到XML(C),c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,我想在我的对象结构中反映XML树,但我是LINQ to XML的初学者 我有一个具有以下结构的XML: <questions> <question id="q1"> <number>1</number> <text>some text11</text> <answers> <answer> <v>some text11</v> <

我想在我的对象结构中反映XML树,但我是LINQ to 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();
当然,通过这种方式,每次我都会从添加到列表中的所有问题中获得所有答案。 如何解决这个问题?我可以想象这很简单,但我不知道:


提前谢谢你

您的代码不起作用,因为您没有根据答案元素的来源对其进行限制,所以您将返回所有答案元素。可以添加此限制,也可以基于问题元素本身进行子查询,而不是基于文档的子查询

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

问题似乎是,如果要将xdoc更改为:

from answer in xml.Elements("answers").Elements("answer")

你应该没事的。这应该是可行的,因为xml包含question元素

你们非常接近。在中,在类名后选择不需要的新零件。您还希望使用.degents而不是.Elements。唯一的另一部分是答案应该使用XMLVAR,而不是返回到原始文档,这将为您提供与问题相关的答案

List<Question> questions = (from xml in xdoc.Descendants("question")
                                    select new Question
                                    {
                                        text = xml.Element("text").Value,
                                        answers =
                                            (from anwsers in xml.Descendants("answer")
                                             select new Answer
                                             {
                                                 Content = anwsers.Element("v").Value
                                             }

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

你上次的陈述是什么意思?创建对同一个SelectIterator的新引用不会导致它执行查询。Samuel,要正确分配它,该行将如下所示:List myQuestions=questions.ToList只能通过调用.ToList或在foreach中使用查询来执行。您应该删除该语句。因为它建议您要让它执行,您必须执行列表temp=..ToList;然后列出问题=temp;。接得好,谢谢萨缪尔。我已经习惯于使用var并在以后获取值,我完全忘记了.ToList已经在语句中了。使用LINQ解析XML比使用XmlDocument有什么优势吗?