C# 在XML的另一个子元素中获取子元素

C# 在XML的另一个子元素中获取子元素,c#,xml,windows-phone-7,linq-to-xml,C#,Xml,Windows Phone 7,Linq To Xml,我有一个XML文件,它是这样的。。。(XML文件是在向web服务[WCF]传递了一些值后从中获取的。) 文件背后的实际代码,也是查询区域,我不知道如何调用,如果他们有另一个子部分: void client_GetQuestionCompleted(object sender, GetQuestionCompletedEventArgs e) { if (e.Error != null) return; string result = e.Result.No

我有一个XML文件,它是这样的。。。(XML文件是在向web服务[WCF]传递了一些值后从中获取的。)

文件背后的实际代码,也是查询区域,我不知道如何调用,如果他们有另一个子部分:

void client_GetQuestionCompleted(object sender, GetQuestionCompletedEventArgs e)
{
     if (e.Error != null)
         return;

     string result = e.Result.Nodes[0].ToString();
     XDocument doc = XDocument.Parse(result);

     var QuestionDetails = from Query in doc.Descendants("QuestionDetail")
                           select new Questions
                           {
                                QuestionID = (int)Query.Element("QuestionID"),
                                QuestionType = (string)Query.Element("QuestionType"),
                                Question = (string)Query.Element("Question"),
                                SubQuestionSequence = (string)Query.Element("SubQuestionSequence")
                           };

     int z = 0;
     foreach (var QuestionDetail in QuestionDetails)
     {
           qID = QuestionDetail.QuestionID;
           qType = QuestionDetail.QuestionType;
           quest = QuestionDetail.Question;
           subQS = QuestionDetail.SubQuestionSequence;

           z++;

     }
}
正如您从顶部看到的,我如何获取子问题的子元素(关键字和ParentQuestionID),其中子问题本身已经是子元素了

[编辑]如何检索子元素中的重复元素?我想让一些部分循环并检索数据,而另一些部分不需要循环来检索数据

   int z = 0;
   foreach (var QuestionDetail in QuestionDetails)
   {
        qID = QuestionDetail.QuestionID;
        qType = QuestionDetail.QuestionType;
        quest = QuestionDetail.Question;
        subQS[z] = QuestionDetail.SubQuestionSequence;
        //doing it this way, i can only retrieve one row of record only, 
        //even though i used an array to save.
        subKeyword[z] = QuestionDetail.SubQuestion.Keywords;            

        z++;

   }

只要只有一个
子问题
元素,您就可以分别访问
Query.element(“子问题”).element(“关键字”)
Query.element(“子问题”).element(“父问题ID”)

[编辑] 对于使用
子问题
类型对象的类,您只需使用

public class Questions {

    public int QuestionID { get; set; }
    public string QuestionType { get; set; }
    public string Question { get; set; }
    public string SubQuestionSequence { get; set; }            
    public SubQuestion SubQuestion{ get; set; }
}

public class SubQuestion {

    public string Keywords { get ; set ; }
    public int ParentQuestionID { get; set; }

}
然后在查询中,您可以使用

 var QuestionDetails = from Query in doc.Descendants("QuestionDetail")
                       select new Questions
                       {
                            QuestionID = (int)Query.Element("QuestionID"),
                            QuestionType = (string)Query.Element("QuestionType"),
                            Question = (string)Query.Element("Question"),
                            SubQuestionSequence = (string)Query.Element("SubQuestionSequence"),
                            SubQuestion = new SubQuestion() {
                               Keywords = (string)Query.Element("SubQuestions").Element("Keywords"),
                               ParentQuestionID = (int)Query.Element("SubQuestions").Element("ParentQuestionID")
                            }
                       };

你能给我们看看你当前的代码吗?你是说,
?嗨,谢谢你的反馈,我更新了代码。嗨,洛基,如果是你说的方式,我怎么做?我也想这样学习。谢谢你,那正是我想要的。顺便问一下,如果get set方法像我那样做会怎么样?是否可以调用一个类?然后,按照后面的代码一次调用所有元素?很难在注释中编写可读的代码,因此我将用更多的行编辑我的答案。呃,只是一个简单的问题,我如何处理重复的子元素?从我现在的方式来看,它只能检索一组。但是,我想检索2个或更多。请给我一些建议,谢谢<代码>顶部第四个区块,注释后的一行。
oh oh,nvm。我找到了做这件事的方法。
public class Questions {

    public int QuestionID { get; set; }
    public string QuestionType { get; set; }
    public string Question { get; set; }
    public string SubQuestionSequence { get; set; }            
    public SubQuestion SubQuestion{ get; set; }
}

public class SubQuestion {

    public string Keywords { get ; set ; }
    public int ParentQuestionID { get; set; }

}
 var QuestionDetails = from Query in doc.Descendants("QuestionDetail")
                       select new Questions
                       {
                            QuestionID = (int)Query.Element("QuestionID"),
                            QuestionType = (string)Query.Element("QuestionType"),
                            Question = (string)Query.Element("Question"),
                            SubQuestionSequence = (string)Query.Element("SubQuestionSequence"),
                            SubQuestion = new SubQuestion() {
                               Keywords = (string)Query.Element("SubQuestions").Element("Keywords"),
                               ParentQuestionID = (int)Query.Element("SubQuestions").Element("ParentQuestionID")
                            }
                       };