Member Avatar for ragnarok511
ragnarok511

I am creating a quiz generator is supposed to pull the values of the radio buttons that you select and put them in an array which will be compared to an array of correct answers that were pulled from the XML file.

The main question is how can I pull values from these buttons when their name & value are dynamically generated.

Here is the code to generate the buttons from the XML file:

private string ProcessXml(XmlReader xmlReader)
        {
            int secondCounter = 1;
            int counter = 1;
            StringBuilder temp = new StringBuilder();
            int i = 0;
            int[] correctAnswerArray = new int[50];

            while (xmlReader.Read())
            {

                if (xmlReader.NodeType != XmlNodeType.EndElement)
                {
                    if (xmlReader.Name == "CorrectAnswer")
                    {
                        
                        counter++;
                        secondCounter = 1;

                        xmlReader.Skip();
                    }
                    if (xmlReader.Name == "AnswerOne" || xmlReader.Name == "AnswerTwo" || xmlReader.Name == "AnswerThree" || xmlReader.Name == "AnswerFour")
                    { //as you can see the name is the would be q then the questions number (i.e. q1, q2, etc), then the value would be 1-4
                        //a group of 4 buttons use the same id but have different values
                        temp.Append("<input type = \"radio\" name = \"q" + counter + "\" value = \"" + secondCounter + "\" "); 
                        secondCounter++;
                    }
                    if (xmlReader.NodeType == XmlNodeType.Text)
                    {
                        temp.Append("<label>" + xmlReader.Value + "</label><br/>"); //generates a label beside each radio button
                    }
                }

            }
            return temp.ToString();
        }
    }

Here is what I have put in my page_load and submit_click section:

public partial class questions : System.Web.UI.Page
    {
        int[] correctAnswers = new int[50]; //initializes arrays for comparision
        int[] yourAnswers = new int[50]; 

        protected void Page_Load(object sender, EventArgs e)
        {
            XmlDisplay xmlDisplayDemo = new XmlDisplay();

            form1.InnerHtml = xmlDisplayDemo.loadDocument(Server.MapPath("xml_quizzes\\quiz21.xml")); //loads the document and radio buttons
            Button submit = new Button(); //creates the submit button
            submit.Text = "Submit";
            submit.Click += submit_Click;
            form1.Controls.Add(submit);
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            int i = 0;
            XmlTextReader reader = new XmlTextReader(Server.MapPath("xml_quizzes\\quiz21.xml"));

            while (reader.Read())  //reads out all the correct answers in the XML file and puts them into an array
            {
                if (reader.Name == "CorrectAnswer")
                {
                    correctAnswers[i] = int.Parse(reader.ReadInnerXml());
                    i++;
                }
            }
            reader.Close();

            //can't figure out how to get the values of the radio buttons selected and read them into the other array 
        }
    }

So, I need a way to go through all the question's answers and pull out the selected values and the array part I can to do on my own. Sorry for the long question. Thanks