1

我正在创建一个采访网页,用户可以在其中回答文本框内屏幕上提到的特定问题。我有一个检查按钮。单击此按钮时,我需要比较在特定问题文本框中输入的答案,并且通常通过与我的 XML 文件对特定问题的答案进行比较来以百分比显示答案的准确性。

这是我的 XML 文件”

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

这是我的表格:

在此处输入图像描述

下面是我的检查按钮的代码,我只比较了一个标签和文本框。有用。

XmlDocument docQuestionList = new XmlDocument();// Set up the XmlDocument //
docQuestionList.Load(@"C:\Users\Administrator\Desktop\questioon\questioon\QuestionAnswer.xml"); //Load the data from the file into the XmlDocument //
XmlNodeList AnswerList = docQuestionList.SelectNodes("Questions/Question");
foreach (XmlNode Answer in AnswerList)
{
    if (Answer.InnerText.Trim() == Label2.Text)
    {
        string[] arrUserAnswer = TextBox1.Text.Trim().ToLower().Split(' ');
        string[] arrXMLAnswer = Answer.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)
        {
            TextBox1.Text = ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%";
        }
        else
        {
            TextBox1.Text = "0 %";
        }
    }
}

如您所见,我只比较了一个问题和相应答案的值,但我希望这不是硬编码的。相反,它应该接受问题和相应的文本框答案并与我的 XML 文件进行比较。我怎样才能实现我的目标?

4

1 回答 1

2

我只是保持简单,并设置一系列问题、答案和用户的答案......

XDocument xdoc = XDocument.Load(@"C:\Users\Administrator\Desktop\questioon\questioon\QuestionAnswer.xml");
string[] questions = xdoc.Root.Elements("Question").Select(x => (string)x).ToArray();
string[] answers = xdoc.Root.Elements("Answer").Select(x => (string)x).ToArray();
string[] userAnswers = new string[] { TextBox1.Text, TextBox2.Text, TextBox3.Text };
for (int i=0 ; i < questions.Length ; i++)
{
    // handle responses
    string[] words = answers[i].Split(' ', StringSplitOptions.RemoveEmptyEntries)
        .Select(w => w.ToLower().Trim()).ToArray();
    string[] userWords = userAnswers[i].Split(' ', StringSplitOptions.RemoveEmptyEntries)
        .Select(w => w.ToLower().Trim()).ToArray();
    string[] correctWords = words.Intersect(userWords);

    // do percentage calc using correctWords.Length / words.Length
}
于 2012-05-20T06:50:29.673 回答