c# - 从 XML 文件中读取元素

标签 c# xml

我正在尝试读取 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>

这是我正在使用的类(class)。问题出在 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;

其余看起来不错。 (我找不到您定义测验的位置)。我尝试了代码,它正在使用 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();

这是假设您有一个类 Question,其属性公开为 question、answerA ... 等等。

关于c# - 从 XML 文件中读取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788801/

相关文章:

c# - 在特定节点后插入 XElements

c# - 删除 XML 中的流氓符号的最佳方法是什么?

c# - 解压缩字节时抛出 System.OutOfMemory 异常

c# - 如何通过反射调用带有枚举(enum)参数的方法?

c# - DotNetOpenAuth:如何实现简单的 OpenId 提供程序?

c# - FromBody 收到的帖子导致可序列化错误

c - LibXML 内部和输出编码

c# - 如何在 Windows Phone 应用程序中以编程方式更改堆栈面板的背景颜色?

xml - 如何将 HTML R 对象转换为字符?

java - XML 模式验证字符串的长度