1

i m creating an web application in that i m matching the user answer with my xml answer i have done all code and my code work fine then after i have changed my xml format so now i m unable to read the attribute of my xml file node. and below is my xml file.

<?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> 

and below is my snipped code.

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");

the above line you can see that i have read the question node but inside the question node there is attribute like Text in that my question present you can see in my xml file.

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

in the above line i m matching the screen display question with my xml file question but i can't do that.label2 is used for displaying the question

help me please.

Sameer Shaikh
  • 67
  • 1
  • 3
  • 12

1 Answers1

0

Try this (using System.Linq; using System.Xml; using 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";
                }
            }
        }
Likurg
  • 2,742
  • 17
  • 22