我有一个是这样的一个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++;
}
你能告诉我们你当前的代码吗? – svick
您是不是指“ ... SubQuestion> ... SubQuestion> SubQuestions>'? –
hihi,感谢您的反馈,我更新了代码。 – user935991