0

我已经创建了一个 asp.net 页面,它正在读取一些文本的 xml 文件节点。下面是我的 xml 文件的样子。

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
  <Question id="1">What is IL code </Question>
  <Answer1>Half compiled,Partially compiled code </Answer1>
  <Question id="2">What is TL code </Question>
  <Answer2>Half compiled,Partially compiled code </Answer2>
</Questions>

我还创建了一个 .aspx 页面,该页面有一个显示问题的标签和一个文本,用户可以在其中输入他/她对该特定问题的答案,并且在一个按钮下方有一些代码,如下所示

    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() == lblQuestion.Text)
        {
            if (Answer.NextSibling.InnerText.Trim() == txtUserAnswer.Text)
            {
                // This is right Answer
                TextBox1.Text = "right";
            }
            else
            {
                // This is wrong Answer
                TextBox1.Text = "wrong";
            }
        }
    }

我想显示用户为特定问题输入的答案的百分比。

例如假设问题是......什么是 IL 代码?并且用户输入答案作为部分编译..所以我只想检查我的 xml 答案节点中输入的关键字。

如果用户答案与节点答案匹配,则以百分比显示答案的准确性。

请帮忙...

谢谢,

4

1 回答 1

1

如评论中所述,所有答案元素都应具有相同的标签。

如果不限制使用Xlinq并且答案标签的格式为 AnswerXXX,则以下代码用于确定问题、答案和答案的百分比(总问题/总答案)和正确答案的百分比(正确答案/总答案) .

您可以根据您的确切需要自定义比较逻辑。

        var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
        "<Questions>" +
        "<Question id=\"1\">What is IL code </Question>" +
        "<Answer1>Half compiled,Partially compiled code </Answer1>"+
        "<Question id=\"2\">What is TL code </Question>"+
        "<Answer2>Half compiled,Partially compiled code1 </Answer2>"+
        "</Questions>";

        var correctAnswerText1 = "Half compiled,Partially compiled code ";// set it to txtUserAnswer.Text


        XElement root= XElement.Parse(xml); // Load String to XElement
        var questions = root.Elements("Question"); // All questions tag
        var answers = root.Elements().Where(e=> e.Name.LocalName.Contains("Answer")); //All answers tag
        var correctAnswers = answers.Where( e=> !e.IsEmpty && String.Equals(e.Value, correctAnswerText1)); // All correct answers, here answer comparision logic can be customized

        var answerPercent = questions.Count()*100/answers.Count();
        var correctAnswerPercent = questions.Count()*100/answers.Count();
        questions.Dump();
        answers.Dump();
        correctAnswers.Dump();
        percantage.Dump();
        //root.Dump();
于 2012-05-13T03:05:05.607 回答