簡體   English   中英

從XML文件中讀取元素

[英]Read elements from XML file

我正在嘗試從XML文件中讀取並使用它來填充我創建的問題對象。 這是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>

這是我正在使用的課程。 問題出在loadQuestions()方法中。

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();
        }

    }

我發現的問題是theQuiz.ChildNodes.Count = 0。

誰知道我哪里出錯了?

在你的代碼中更改theQuiz = doc.FirstChild;

theQuiz = doc.LastChild;

其余看起來很好。 (我找不到你定義theQuiz的地方)。 我嘗試了代碼,它正在使用var theQuiz = doc.LastChild;

如果您想使用LINQ,那么您可以嘗試以下方法:

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

這假設您有一個類問題,其屬性公開為問題,answerA ...等等。

使用LINQ to XML:

使用LINQ to XML:

XDocument doc = XDocument.Load("YOURXML.xml");
var quiz = from elements in doc.Elements("quiz").Elements("problem")
           select elements;

foreach (var item in quiz)
{
question = item.Element("question").Value;
                    a = item.Element("answerA").Value; 
                    b = item.Element("answerB").Value; 
                    c = item.Element("answerC").Value; 
                    d = item.Element("answerD").Value; 
                    correct = item.Element("correct").Value; 
 questions.Add(new Question(question, a, b, c, d, correct));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM