1

我正在创建一个 Web 应用程序,我将用户答案与我的 xml 答案匹配我已经完成了所有代码并且我的代码工作正常,然后在我更改了我的 xml 格式之后,所以现在我无法读取我的 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> 

下面是我的剪切代码。

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)
            {
                if (**nodexm.InnerText.Trim()** == 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.Visible = true;
                        label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                    }
                    else
                    {
                        label4.Text = "Your Answer is Wrong";
                    }
                }
            }

XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");

上面的行你可以看到我已经阅读了问题节点,但是在问题节点内部有像 Text 这样的属性,我的问题存在你可以在我的 xml 文件中看到。

if (nodexm.InnerText.Trim() == label2.Text)

在上面的行中,我将屏幕显示问题与我的 xml 文件问题匹配,但我做不到。label2 用于显示问题

请帮帮我。

4

1 回答 1

0

试试这个(使用 System.Linq;使用 System.Xml;使用 System.Xml.Linq;)

 XDocument map = XDocument.Parse("<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>");
        var atts = map.Descendants("Question").Attributes().Where(a => a.Name == "Text").ToList();
        var QuestionList = map.Descendants("Question").ToList();
        foreach (XElement nodexm in QuestionList)
        {
            if((string)nodexm.Attributes("Text").FirstOrDefault()== label2.Text)
            {
                string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
                string[] arrXMLAnswer = nodexm.Elements("Answer").SelectMany(o => ((string)o.Attribute("Text")).Split(',')).ToArray();
                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.Visible = true;
                    label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
                }
                else
                {
                    label4.Text = "Your Answer is Wrong";
                }
            }
        }
于 2012-05-29T08:17:56.500 回答