c# - 获取 XML 的另一个子元素中的子元素

标签 c# xml windows-phone-7 linq-to-xml

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

<Title>
  <Questions>
    <QuestionID> 1 </QuestionID>
    <QuestionType> Quiz </QuestionType>
    <Question> What is the shape? </Question>
    <SubQuestionSequence> Part 1 </SubQuestionSequence>
    <SubQuestions>
           <Keywords> Ring </Keywords>
           <ParentQuestionID> 1 </ParentQuestionID>
    </SubQuestions>
    <SubQuestionSequence> Part2 </SubQuestionSequence>
    <SubQuestions>
           <Keywords> Round </Keywords>
           <ParentQuestionID> 1 </ParentQuestionID>             
    </SubQuestions>
  </Questions>
</Title>

获取子元素的方法如下(C#写的),注释区应该调用subQuestion的类,但是我不知道那部分怎么写:

public class Questions {

    public int QuestionID { get; set; }
    public string QuestionType { get; set; }
    public string Question { get; set; }
    public string SubQuestionSequence { get; set; }            
    //suppose to call subQuestion here 
}

public class SubQuestion {

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

}

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

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++;

     }
}

正如您从顶部看到的那样,我如何获取 SubQuestions 的子元素(关键字和 ParentQuestionID),其中 SubQuestion 本身已经是一个子元素?

[edit] 如何检索子元素中的重复元素?我想要一些部分循环和检索数据,一些不需要循环检索。

   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++;

   }

最佳答案

只要只有一个 SubQuestions 元素,您就可以分别访问 Query.Element("SubQuestions").Element("Keywords") Query.Element("SubQuestions").Element("ParentQuestionID").

[编辑] 至于你使用 SubQuestion 类型的对象进行分类,你只需使用

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")
                            }
                       };

关于c# - 获取 XML 的另一个子元素中的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7545821/

相关文章:

c# - 将新创建的对象传递给多个 Storyboard

c# - 不同程序集中系统命名空间类型和用户定义类型之间的命名冲突

c# - 使用 Autofac 4 和 vNext 自注册库

email - 如何从 Windows Phone 7 发送电子邮件?

c# - SQL Server LocalDB 根本无法创建数据库

xml - 使用 XSLT 转换 EntityObject XML 的问题

android - 在 Android 中缓存从 XML 解析的对象

silverlight - 如何在wp7中动态添加listbox.itemssource中的元素?

c# - 使用 JSON.net 反序列化 JSON 流

java - JAXB 命名空间和前缀