XML循环遍历C ++ / CLI中的所有子级和子级

时间:2022-09-01 12:34:45

At the moment, I'm using XML in order to create a data structure containing many questions for a quiz containing info like the question itself, the answer etc and then for those to be put into variables to be displayed in labels. Here's an example of the XML file:

目前,我正在使用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>

Now, I'm intending to have a good number of questions contained in this file. However, the further up the quiz you progress, the harder they get, hence I have included an element called level with a number corresponding to when it will be asked (so for example, you have 15 questions in the quiz, you are on question 3 so it parses the XML file for a question that has the level element as 3, then loads the data into the program).

现在,我打算在这个文件中包含大量问题。然而,你进步的测验越进一步,他们就越难获得,因此我添加了一个名为level的元素,其数字对应于何时被询问(例如,你在测验中有15个问题,你有问题3因此它解析XML文件以获得level元素为3的问题,然后将数据加载到程序中)。

However, there's a problem - most likely a logic error. In the C++/CLI code, I have this:

但是,存在一个问题 - 很可能是逻辑错误。在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;
         }

As I try to loop through the list of questions (questionData being the parent, question being the child and all the data contained in question being the subchilds), when I try to display the variable in the label, I get no text at all, hence leading me to believe I've made a logic error in the loop. I'm relatively new to XML, so I'm unable to spot my error. Are any of you able to help me out or possibly know of a more efficient way to perform the aforementioned task? Thanks.

当我尝试循环遍历问题列表(questionData是父项,问题是孩子,所有包含的数据都是子项),当我尝试在标签中显示变量时,我根本没有文本,因此让我相信我在循环中犯了一个逻辑错误。我对XML比较新,所以我无法发现错误。您是否有人能够帮助我或者可能知道更有效的方式来执行上述任务?谢谢。

1 个解决方案

#1


Your root element (respectively documentElement) is already the questionData element so doing root->GetElementsByTagName("questionData") returns an empty list. If you want to process the question elements in a loop then simply process for each(question in questionsData->GetElementsByTagName("question")) and then inside the loop make sure you call the method on the loop variable e.g. levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;.

您的根元素(分别是documentElement)已经是questionData元素,因此执行root-> GetElementsByTagName(“questionData”)会返回一个空列表。如果你想在循环中处理问题元素,那么只需处理每个问题(questionsData-> GetElementsByTagName(“question”)中的问题)然后在循环内部确保你在循环变量上调用方法,例如: levelFromXML =(question-> GetElementsByTagName(“level”)) - > Item(0) - > InnerText;。

I would also use https://msdn.microsoft.com/en-us/library/sss31aas%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 instead of GetElementsByTagName inside of the loop, so you can replace the above snippet simply with levelFromXML = question["level"]->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;替换上面的代码片段。

#1


Your root element (respectively documentElement) is already the questionData element so doing root->GetElementsByTagName("questionData") returns an empty list. If you want to process the question elements in a loop then simply process for each(question in questionsData->GetElementsByTagName("question")) and then inside the loop make sure you call the method on the loop variable e.g. levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;.

您的根元素(分别是documentElement)已经是questionData元素,因此执行root-> GetElementsByTagName(“questionData”)会返回一个空列表。如果你想在循环中处理问题元素,那么只需处理每个问题(questionsData-> GetElementsByTagName(“question”)中的问题)然后在循环内部确保你在循环变量上调用方法,例如: levelFromXML =(question-> GetElementsByTagName(“level”)) - > Item(0) - > InnerText;。

I would also use https://msdn.microsoft.com/en-us/library/sss31aas%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 instead of GetElementsByTagName inside of the loop, so you can replace the above snippet simply with levelFromXML = question["level"]->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;替换上面的代码片段。