XML 在 C++/CLI 中循环遍历所有子项和子项
XML Loop through all childs and subchilds in C++/CLI
目前,我正在使用 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>
现在,我打算在此文件中包含大量问题。然而,你进行的测验越深入,他们就越难,因此我包含了一个名为 level 的元素,其中包含一个对应于何时被问到的数字(例如,你在测验中有 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 是父项,question 是子项,问题中包含的所有数据是子项),当我尝试在标签中显示变量时,我得到根本没有文字,因此让我相信我在循环中犯了一个逻辑错误。我是 XML 的新手,所以我无法发现我的错误。你们中的任何人都能够帮助我或可能知道执行上述任务的更有效方法吗?谢谢
您的根元素(分别为 documentElement)已经是 questionData
元素,因此 root->GetElementsByTagName("questionData")
returns 是一个空列表。如果要在循环中处理 question
元素,则只需处理 for each(question in questionsData->GetElementsByTagName("question"))
,然后在循环内确保调用循环变量上的方法,例如levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;
。
我还会在循环内使用 https://msdn.microsoft.com/en-us/library/sss31aas%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 而不是 GetElementsByTagName
,因此您可以简单地用 levelFromXML = question["level"]->InnerText;
.
替换上面的代码片段
目前,我正在使用 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>
现在,我打算在此文件中包含大量问题。然而,你进行的测验越深入,他们就越难,因此我包含了一个名为 level 的元素,其中包含一个对应于何时被问到的数字(例如,你在测验中有 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 是父项,question 是子项,问题中包含的所有数据是子项),当我尝试在标签中显示变量时,我得到根本没有文字,因此让我相信我在循环中犯了一个逻辑错误。我是 XML 的新手,所以我无法发现我的错误。你们中的任何人都能够帮助我或可能知道执行上述任务的更有效方法吗?谢谢
您的根元素(分别为 documentElement)已经是 questionData
元素,因此 root->GetElementsByTagName("questionData")
returns 是一个空列表。如果要在循环中处理 question
元素,则只需处理 for each(question in questionsData->GetElementsByTagName("question"))
,然后在循环内确保调用循环变量上的方法,例如levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;
。
我还会在循环内使用 https://msdn.microsoft.com/en-us/library/sss31aas%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 而不是 GetElementsByTagName
,因此您可以简单地用 levelFromXML = question["level"]->InnerText;
.