C# 如何比较用户';我的XML文件有多个答案?

C# 如何比较用户';我的XML文件有多个答案?,c#,asp.net,xml,C#,Asp.net,Xml,我正在创建一个访谈网页,用户可以在其中回答文本框内屏幕上提到的特定问题。我有一个检查按钮。单击此按钮时,我需要比较在特定问题文本框中输入的答案,通常通过与XML文件中特定问题的答案进行比较,以百分比显示答案的准确性 这是我的XML文件“ 什么是IL代码 半编译、半编译代码 什么是准时制 机器语言的IL代码 什么是CLR 引擎的心脏、GC、编译、CAS(代码访问安全)、CV(代码验证) 这是我的表格: 下面是我的复选按钮代码,我只比较了一个标签和文本框。它很有效 XmlDocument do

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

这是我的XML文件“


什么是IL代码
半编译、半编译代码
什么是准时制
机器语言的IL代码
什么是CLR
引擎的心脏、GC、编译、CAS(代码访问安全)、CV(代码验证)
这是我的表格:

下面是我的复选按钮代码,我只比较了一个标签和文本框。它很有效

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 %";
        }
    }
}
XmlDocument docQuestionList=new XmlDocument();//设置XmlDocument//
docQuestionList.Load(@“C:\Users\Administrator\Desktop\questioon\questioon\QuestionAnswer.xml”);//将文件中的数据加载到XmlDocument中//
XmlNodeList AnswerList=docQuestionList.SelectNodes(“问题/问题”);
foreach(应答列表中的XmlNode应答)
{
if(Answer.InnerText.Trim()==Label2.Text)
{
字符串[]arrUserAnswer=TextBox1.Text.Trim().ToLower().Split(“”);
string[]arrXMLAnswer=Answer.NextSibling.InnerText.Trim().ToLower().Split(“”);
List lststr1=新列表();
foreach(arrmmlanswer中的字符串nextStr)
{
if(Array.IndexOf(arrUserAnswer,nextStr)!=-1)
{
lststr1.Add(nextStr);
}
}
如果(lststr1.Count>0)
{
TextBox1.Text=((100*lststr1.Count)/arrXMLAnswer.Length.ToString()+“%”;
}
其他的
{
TextBox1.Text=“0%”;
}
}
}

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

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

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
}
XDocument xdoc=XDocument.Load(@“C:\Users\Administrator\Desktop\questioon\questioon\QuestionAnswer.xml”);
string[]questions=xdoc.Root.Elements(“问题”)。选择(x=>(string)x.ToArray();
字符串[]answers=xdoc.Root.Elements(“Answer”)。选择(x=>(字符串)x.ToArray();
string[]userAnswers=新字符串[]{TextBox1.Text,TextBox2.Text,TextBox3.Text};
for(int i=0;iw.ToLower().Trim()).ToArray();
string[]userWords=userAnswers[i]。拆分(“”,StringSplitOptions.RemoveEmptyEntries)
.选择(w=>w.ToLower().Trim()).ToArray();
string[]correctWords=words.Intersect(userWords);
//使用correctWords.Length/words.Length进行百分比计算
}

@aamankhan又添加了一些代码(我使用Linq Intersect方法来计算正确单词的数量)。
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
}