Exemple #1
0
        private void Load_Quest()
        {
            int         c   = 0;
            XmlDocument doc = new XmlDocument();

            doc.Load(@"..\..\Test\Questions.xml");
            XmlElement xRoot = doc.DocumentElement;

            foreach (XmlNode xnode in xRoot) //Все Тесты
            {
                if (xnode.Attributes.Count > 0)
                {
                    XmlNode attr = xnode.Attributes.GetNamedItem("name");
                    if (attr.InnerText == testName) // Если это наш тест
                    {
                        foreach (XmlNode childNode in xnode.ChildNodes)
                        {
                            QuestionsAndAnswers question = new QuestionsAndAnswers();
                            if (xnode.Attributes.Count > 0)
                            {
                                XmlNode attr1 = childNode.Attributes.GetNamedItem("name"); // Вопрос
                                if (attr1 != null)
                                {
                                    question.question = attr1.Value;
                                    foreach (XmlNode childChildNode in childNode.ChildNodes) // Ответы на него
                                    {
                                        if (childChildNode.Name == "answer1")
                                        {
                                            question.answer1 = childChildNode.InnerText;
                                        }
                                        else if (childChildNode.Name == "answer2")
                                        {
                                            question.answer2 = childChildNode.InnerText;
                                        }
                                        else if (childChildNode.Name == "answer3")
                                        {
                                            question.answer3 = childChildNode.InnerText;
                                        }
                                        else if (childChildNode.Name == "answer4")
                                        {
                                            question.answer4 = childChildNode.InnerText;
                                        }
                                        else if (childChildNode.Name == "correctAnswer")
                                        {
                                            question.trueAnswer = childChildNode.InnerText;
                                        }
                                        c++;
                                        if (c == 5)
                                        {
                                            c = 0;
                                            answers.Add(question);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void SaveNewQuest()
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(@"..\..\Test\Questions.xml");
            XmlElement xRoot = doc.DocumentElement;
            // создаем атрибут name для question
            XmlAttribute nameAttr2 = doc.CreateAttribute("name");
            // создаем элемент question
            XmlElement question = doc.CreateElement("question");
            // Answers
            XmlElement answer1       = doc.CreateElement("answer1");
            XmlElement answer2       = doc.CreateElement("answer2");
            XmlElement answer3       = doc.CreateElement("answer3");
            XmlElement answer4       = doc.CreateElement("answer4");
            XmlElement correctAnswer = doc.CreateElement("correctAnswer");
            // создаем текстовые значения для элементов и атрибута
            XmlText nameText2          = doc.CreateTextNode(tbxQuestion.Text);
            XmlText answer1Text        = doc.CreateTextNode(tbxAnswer1.Text);
            XmlText answer2Text        = doc.CreateTextNode(tbxAnswer2.Text);
            XmlText answer3Text        = doc.CreateTextNode(tbxAnswer3.Text);
            XmlText answer4Text        = doc.CreateTextNode(tbxAnswer4.Text);
            XmlText CorrectAnswer1Text = doc.CreateTextNode(tbxAnswer1.Text);
            XmlText CorrectAnswer2Text = doc.CreateTextNode(tbxAnswer2.Text);
            XmlText CorrectAnswer3Text = doc.CreateTextNode(tbxAnswer3.Text);
            XmlText CorrectAnswer4Text = doc.CreateTextNode(tbxAnswer4.Text);

            foreach (XmlNode xnode in xRoot)
            {
                if (xnode.Attributes.Count > 0)
                {
                    XmlNode attr = xnode.Attributes.GetNamedItem("name");
                    if (attr.InnerText == testName)
                    {
                        QuestionsAndAnswers quest = new QuestionsAndAnswers();
                        quest.question = tbxQuestion.Text;
                        nameAttr2.AppendChild(nameText2);      // name - question
                        question.Attributes.Append(nameAttr2); // Attribut name - "question?"
                        answer1.AppendChild(answer1Text);      // Text - answer
                        quest.answer1 = tbxAnswer1.Text;
                        answer2.AppendChild(answer2Text);      // Text - answer
                        quest.answer2 = tbxAnswer2.Text;
                        answer3.AppendChild(answer3Text);      // Text - answer
                        quest.answer3 = tbxAnswer3.Text;
                        answer4.AppendChild(answer4Text);      // Text - answer
                        quest.answer4 = tbxAnswer4.Text;
                        if (ans == 0)
                        {
                            correctAnswer.AppendChild(CorrectAnswer1Text);// Text - CorrectAnswer
                            quest.trueAnswer = tbxAnswer1.Text;
                        }
                        if (ans == 1)
                        {
                            correctAnswer.AppendChild(CorrectAnswer2Text);// Text - CorrectAnswer
                            quest.trueAnswer = tbxAnswer2.Text;
                        }
                        if (ans == 2)
                        {
                            correctAnswer.AppendChild(CorrectAnswer3Text);// Text - CorrectAnswer
                            quest.trueAnswer = tbxAnswer3.Text;
                        }
                        if (ans == 3)
                        {
                            correctAnswer.AppendChild(CorrectAnswer4Text);// Text - CorrectAnswer
                            quest.trueAnswer = tbxAnswer4.Text;
                        }
                        answers.Add(quest);
                        // Добавляем в узел question - Answers
                        question.AppendChild(answer1);       // answer
                        question.AppendChild(answer2);       // answer
                        question.AppendChild(answer3);       // answer
                        question.AppendChild(answer4);       // answer
                        question.AppendChild(correctAnswer); // CorrectAnswer
                        xnode.AppendChild(question);
                    }
                }
            }
            doc.Save(@"..\..\Test\Questions.xml");
            newQuest = false;
        }