目前,我正在使用XML来创建一个数据结构,其中包含许多问题,其中包含诸如问题本身,答案等的信息,然后将其放置将变量显示在标签中。这是XML文件的示例:
<?xml version="1.0" encoding="utf-8"?>
<questionData>
<question>
<level> 1 </level>
<ref> ref1 </ref>
<questionText>This is a test question using XML!</questionText>
<A>This is A using XML!</A>
<B>This is B using XML!</B>
<C>This is C using XML!</C>
<D>This is D using XML!</D>
<corrAnswer> A </corrAnswer>
<APerc> 97 </APerc>
<BPerc> 1 </BPerc>
<CPerc> 1 </CPerc>
<DPerc> 1 </DPerc>
<PAFAnswer> A </PAFAnswer>
<PAFFeeling> Sure </PAFFeeling>
<FF> B </FF>
<FFcorrPerc> 100 </FFcorrPerc>
<FFwrongPerc> 0 </FFwrongPerc>
<FFPAFAnswer> A </FFPAFAnswer>
<FFPAFFeeling> Sure </FFPAFFeeling>
<EXP> This is a test explanation for the info box. </EXP>
<pronuns> pro-nun-cee-a-sh-un </pronuns>
</question>
<question>
<level> 2 </level>
<ref> ref2 </ref>
<questionText>This is another test question using XML!</questionText>
<A>This is A2 using XML!</A>
<B>This is B2 using XML!</B>
<C>This is C2 using XML!</C>
<D>This is D2 using XML!</D>
<corrAnswer> B </corrAnswer>
<APerc> 96 </APerc>
<BPerc> 1 </BPerc>
<CPerc> 1 </CPerc>
<DPerc> 2 </DPerc>
<PAFAnswer> B </PAFAnswer>
<PAFFeeling> Sure </PAFFeeling>
<FF> A </FF>
<FFcorrPerc> 100 </FFcorrPerc>
<FFwrongPerc> 0 </FFwrongPerc>
<FFPAFAnswer> B </FFPAFAnswer>
<FFPAFFeeling> Sure </FFPAFFeeling>
<EXP> This is another test explanation for the info box. </EXP>
<pronuns> pro-nun-cee-a-sh-un two </pronuns>
</question>
</questionData>
现在,我打算在此文件中有很多问题。但是,进步的测验越远,他们越难获得,因此我包括了一个称为级别的元素,该元素的数字与何时相对应的数字(例如,您在测验中有15个问题,您有问题3因此,它将XML文件解析为一个具有级别元素为3的问题,然后将数据加载到程序中)。
但是,有一个问题 - 很可能是逻辑错误。在C /CLI代码中,我有:
XmlTextReader^ dataFromQFile = gcnew XmlTextReader("Millionaire\questionsData.xml");
XmlDocument^ questionsData = gcnew XmlDocument();
questionsData->Load("Millionaire\questionsData.xml");
XmlElement^ root = questionsData->DocumentElement;
XmlNodeList^ listOfQuestions = root->GetElementsByTagName("questionData");
XmlNodeList^ Questions = root->GetElementsByTagName("question");
for each (Questions in listOfQuestions)
{
levelFromXML = (root->GetElementsByTagName("level"))->Item(0)->InnerText;
questionFromXML = (root->GetElementsByTagName("questionText"))->Item(0)->InnerText;
AFromXML = (root->GetElementsByTagName("A"))->Item(0)->InnerText;
BFromXML = (root->GetElementsByTagName("B"))->Item(0)->InnerText;
CFromXML = (root->GetElementsByTagName("C"))->Item(0)->InnerText;
DFromXML = (root->GetElementsByTagName("D"))->Item(0)->InnerText;
corrFromXML = (root->GetElementsByTagName("corrAnswer"))->Item(0)->InnerText;
APercFromXML = (root->GetElementsByTagName("APerc"))->Item(0)->InnerText;
BPercFromXML = (root->GetElementsByTagName("BPerc"))->Item(0)->InnerText;
CPercFromXML = (root->GetElementsByTagName("CPerc"))->Item(0)->InnerText;
DPercFromXML = (root->GetElementsByTagName("DPerc"))->Item(0)->InnerText;
phoneAnswerFromXML = (root->GetElementsByTagName("PAFAnswer"))->Item(0)->InnerText;
phoneFeelingFromXML = (root->GetElementsByTagName("PAFFeeling"))->Item(0)->InnerText;
fiftyAnswer = (root->GetElementsByTagName("FF"))->Item(0)->InnerText;
fiftyCorrPerc = (root->GetElementsByTagName("FFcorrPerc"))->Item(0)->InnerText;
fiftyWrongPerc = (root->GetElementsByTagName("FFwrongPerc"))->Item(0)->InnerText;
fiftyPhoneAnswer = (root->GetElementsByTagName("FFPAFAnswer"))->Item(0)->InnerText;
fiftyPhoneFeeling = (root->GetElementsByTagName("FFPAFFeeling"))->Item(0)->InnerText;
exp = (root->GetElementsByTagName("EXP"))->Item(0)->InnerText;
pronuns = (root->GetElementsByTagName("pronuns"))->Item(0)->InnerText;
}
当我尝试循环浏览问题列表时(QuestionData是父母,问题是孩子,所涉及的所有数据是子孩子),当我尝试在标签中显示变量时,我没有得到文本因此,使我相信我在循环中犯了逻辑错误。我对XML相对较新,因此我无法发现错误。你们中的任何一个能够帮助我,或者可能知道执行上述任务的更有效的方法?谢谢。
您的root元素(分别是纪录片)已经是questionData
元素,因此执行root->GetElementsByTagName("questionData")
返回一个空列表。如果要在循环中处理question
元素,则只需处理for each(question in questionsData->GetElementsByTagName("question"))
,然后在循环内部确保在循环变量上调用方法,例如。levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;
。
我还将使用https://msdn.microsoft.com/en-us/library/sssss31aas(v=vs.110)9.aspx?cs-save-save-save-lang = 1&cs-amps; cs; css-lang = cppp#code-snippet-1而不是 GetElementsByTagName
在循环内部,因此您可以简单地用levelFromXML = question["level"]->InnerText;
替换上述摘要。