1

我正在创建一个窗口应用程序,其中我有一个用于显示问题的标签和一个用于回答相应问题的文本框。然后是 3 个按钮用于使用我的 xml 文件检查答案,一个按钮用于下一个问题,一个按钮用于上一个问题。

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8" ?>
<Exam>
  <Question number="1" Text="What is IL Code">
    <Answer Text="Half compiled, Partially compiled code"> </Answer>
  </Question>
  <Question number="2" Text="What is JIT">
    <Answer Text="IL code to machine language"> </Answer>
  </Question>
  <Question number="3" Text="What is CLR">
    <Answer Text="Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)"> </Answer>
  </Question>
</Exam> 

现在单击按钮,我想用我的 xml 答案检查用户的答案,这部分我已经完成了,但只要第一个问题是什么是 il 代码,我的代码就可以正常工作,当问题改变时,我的代码每次回答第一个问题时都无法回答第二个问题并与之进行比较,那么我该如何实现呢?下面是我截取的代码

string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
            do
            {
                XmlReader reader = XmlReader.Create(@"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml");
                reader.Read();
                reader.ReadToFollowing("Question");
                reader.MoveToContent();
                que = reader.GetAttribute("Text");
                reader.ReadToFollowing("Answer");
                reader.MoveToContent();
                string[] arrXMLAnswer = reader.GetAttribute("Text").ToString().Trim().ToLower().Split(' ');
                List<string> lststr1 = new List<string>();
                if (label2.Text == que)
                {
                    abc = 1;
                    foreach (string nextStr in arrXMLAnswer)
                    {
                        if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
                        {
                            lststr1.Add(nextStr);
                        }
                    }
                    if (lststr1.Count > 0)
                    {
                        label4.Visible = true;
                        label4.Text = "Your Answer is " + ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                    }
                    else
                    {
                        textBox1.Text = "0 %";
                    }


                }
                else
                {
                    reader.ReadToNextSibling("Question");
                }


            } while (abc <= 0);
            abc = 0;

我还为此使用了第二种方法,但是由于我在问题节点中编写了问题,因此代码无法找到我的问题,下面是我的另一个代码。

XmlDocument docQuestionList = new XmlDocument();// Set up the XmlDocument //
            docQuestionList.Load(@"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml"); //Load the data from the file into the XmlDocument //
            XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");
            foreach (XmlNode nodexm in QuestionList)
            {
                string obj = nodexm.SelectNodes("Text").ToString();
                if (obj == label2.Text)
                {
                    string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
                    string[] arrXMLAnswer = nodexm.NextSibling.InnerText.Trim().ToLower().Split(' ');
                    List<string> lststr1 = new List<string>();
                    foreach (string nextStr in arrXMLAnswer)
                    {
                        if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
                        {
                            lststr1.Add(nextStr);
                        }
                    }
                    if (lststr1.Count > 0)
                    {
                        label4.Text = "Your Answer is " + ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                    }
                    else
                    {
                        textBox1.Text = "0 %";
                    }
                }
            }

请以任何一种方法帮助我

4

1 回答 1

2

就像是:

//setup the doc
string fn="C:\\bla.xml";
XmlDocument xmlDocument=new XmlDocument();
xmlDocument.Load(fn);
XmlNode root=xmlDocument.DocumentElement;

//get the node
XmlNode answerNode=root.SelectSingleNode("//Question[@number="+num+"]/Answer");

//get the value
string attrName="Text";
XmlAttribute atr=answerNode.Attributes.GetNamedItem(attrName) as XmlAttribute;
if (atr!=null){
   string answer=atr.value;
}

会让你的生活轻松很多。注意并提供 num 变量,并进行一些空值检查。

于 2012-05-25T12:43:09.640 回答