我有一个类似这样的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++;
}
答案 0 :(得分:1)
只要只有一个SubQuestions
元素,您就可以分别Query.Element("SubQuestions").Element("Keywords")
Query.Element("SubQuestions").Element("ParentQuestionID")
轻松访问。{/ p>
[编辑]
对于具有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")
}
};