Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read elements from XML file

Tags:

c#

xml

I'm trying to read from an XML file and use that to populate a question object I've created. This is the XML:

<?xml version="1.0" encoding="utf-8" ?>
<quiz>
  <problem>
    <question>Which of the following languages could be used in both Visual Studio and Unity?</question>
    <answerA>Cobol</answerA>
    <answerB>C#</answerB>
    <answerC>C−−</answerC>
    <answerD>French</answerD>
    <correct>B</correct>
  </problem>
  <problem>
    <question>What does XML stand for?</question>
    <answerA>eXtremely Muddy Language</answerA>
    <answerB>Xerxes, the Magnificent Chameleon</answerB>
    <answerC>eXtensible Markup Language</answerC>
    <answerD>eXecutes with Multiple Limitations</answerD>
    <correct>C</correct>
  </problem>
</quiz>

This is the class I'm using. The problem is in the loadQuestions() method.

public partial class frmQuestions : Form
    {
        private XmlDocument doc;
        private XmlNode theQuiz;
        private List<Question> questions;
        private Random random;

        public frmQuestions(string docName)
        {
            InitializeComponent();
            doc = new XmlDocument();
            doc.Load(docName);
            questions = new List<Question>();          
            loadQuestions();
            displayQuestion();
        }

        private void frmQuestions_Load(object sender, EventArgs e)
        {

        }

        private void loadQuestions()
        {
            string question, a, b, c, d, correct;
            theQuiz = doc.FirstChild;

            for(int i = 0; i < theQuiz.ChildNodes.Count; i++)
            {
                XmlNode theQuestion = theQuiz.ChildNodes[i];
                question = theQuestion["question"].InnerText;
                a = theQuestion["answerA"].InnerText;
                b = theQuestion["answerB"].InnerText;
                c = theQuestion["answerC"].InnerText;
                d = theQuestion["answerD"].InnerText;
                correct = theQuestion["correct"].InnerText;

                questions.Add(new Question(question, a, b, c, d, correct));
            }
        }

        private void displayQuestion()
        {
            Random random = new Random();
            int randomNumber = random.Next(1, questions.Count);

            lblQuestion.Text = questions[randomNumber].getQuestion();
            lblA.Text = questions[randomNumber].getA();
            lblB.Text = questions[randomNumber].getB();
            lblC.Text = questions[randomNumber].getC();
            lblD.Text = questions[randomNumber].getD();
        }

    }

The problem I'm finding is that theQuiz.ChildNodes.Count = 0.

Anyone know where I'm going wrong?

like image 503
Arcadian Avatar asked Oct 08 '22 17:10

Arcadian


1 Answers

In your code change theQuiz = doc.FirstChild; to

theQuiz = doc.LastChild;

The rest looks fine. (I couldn't find where you have defined theQuiz). I tried the code and it is working with var theQuiz = doc.LastChild;

If you want to use LINQ then you can try the following:

XDocument xDoc = XDocument.Load("XMLFile1.xml");

            var query = (from x in xDoc.Descendants("quiz").Elements("problem")
                        select new Question
                        {
                            question = x.Element("question").Value,
                            answerA = x.Element("answerA").Value,
                            answerB = x.Element("answerB").Value,
                            answerC = x.Element("answerC").Value,
                            answerD = x.Element("answerD").Value,
                            correct = x.Element("correct").Value
                        }).ToList();

This is assuming that you have one class Question with properties exposed as question, answerA ... and so on.

like image 54
Habib Avatar answered Oct 10 '22 08:10

Habib