2011-09-25 124 views
2

我有一个是这样的一个XML文件...(XML文件是从Web服务[WCF]通过采取一定的价值进去后。)获取子元素在XML的另一个子元素

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

    } 
} 

正如你可以从上面看到的,我怎么能采取的子问题(关键字和ParentQuestionID),其中SubQuestion本身就已经是一个子元素的子元素?

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

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

    } 
+0

你能告诉我们你当前的代码吗? – svick

+0

您是不是指“ ... ...'? –

+0

hihi,感谢您的反馈,我更新了代码。 – user935991

回答

1

只要有只有一个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") 
          } 
         }; 
+0

谢谢你,那正是我正在寻找的。顺便说一句,如果get set方法我做了什么?是否有可能召集成一个班级?然后,在后面的代码后面立即调用所有的代码? – user935991

+0

在评论中编写可读代码很困难,所以我会用更多的行编辑我的答案。 –

+0

谢谢你,马丁。 – user935991

相关问题