1

インタビューのWebページを作成しています。このページでは、ユーザーはテキストボックス内の画面に記載されている特定の質問に答えることができます。チェックボタンがあります。このボタンをクリックすると、特定の質問のテキストボックスに入力された回答を比較する必要があります。通常、特定の質問に対する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 %";
        }
    }
}

ご覧のとおり、1つの質問とそれぞれの回答の値を比較しただけですが、これはハードコーディングされていないことをお勧めします。代わりに、質問とそれぞれのテキストボックスの回答を受け取り、私の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 に答える