0

我需要将此 xml 读入一个类。我是 LINQ to XML 的新手。

<quiz>
  <step id="1">
    <question id="1">
      <text>What is the world’s tallest tower?</text>
      <answers>
        <answer id="1" value="0">Eiffel Tower</answer>
        <answer id="2" value="0">Petronas Tower</answer>
        <answer id="3" value="0">Canton Tower</answer>
        <answer id="4" value="1">Tokyo Skytree </answer>
      </answers>
    </question>
  </step>
</quiz>

我已经创建了这两个类,但我不确定如何轻松读取信息。我的头被它毁了。

public class QuizQuestion
{
    public int StepId { get; set; }
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public List<QuizAnswer> Answers { get; set; }
}

public class QuizAnswer
{
    public int AnswerId { get; set; }
    public int CorrectAnswer { get; set; }
    public String AnswerText { get; set; }
}

这个我试过了,不知道对不对

var quizQuestions = new List<QuizQuestion>();

_cacheLock.EnterWriteLock();
try
{
    XDocument xmlDoc = XDocument.Load(_questionsFilePath);

    XDocument data = XDocument.Load(_questionsFilePath);

    quizQuestions = (from c in data.Descendants("quiz")
    orderby c.Attribute("question")
    select new QuizQuestion()
    {
        StepId = Convert.ToInt32(c.Attribute("Id").Value),
        QuestionId = Convert.ToInt32(c.Attribute("Id").Value),
        QuestionText = c.Value
    }).ToList();

    foreach (QuizQuestion quiz in quizQuestions)
    {
        quiz.Answers = 
            (from c in data.Descendants("quiz")
                orderby c.Attribute("question")
                where Convert.ToInt32(c.Attribute("Id").Value) == 1
                select new QuizAnswer()
                {
                    AnswerId = Convert.ToInt32(
                                 c.Attribute("id").Value),
                    AnswerText = c.Value,
                    CorrectAnswer = Convert.ToInt32(
                                      c.Attribute("value").Value)
                }).ToList();
    }

}
catch (NullReferenceException e)
{

}
4

5 回答 5

0

我已经测试了我的并且它有效,包括根据他们的 ID 对问题进行排序。

quizQuestions = (from step in data.Element("quiz").Elements("step")
                 from question in step.Elements("question")
                 orderby question.Attribute("id").Value
                 select new QuizQuestion()
                 {
                     StepId = Convert.ToInt32(step.Attribute("id").Value),
                     QuestionId = Convert.ToInt32(question.Attribute("id").Value),
                     QuestionText = question.Element("text").Value,
                     Answers = (from answer in question.Element("answers").Elements("answer")
                         select new QuizAnswer
                         {
                             AnswerId = Convert.ToInt32(answer.Attribute("id").Value),
                             AnswerText = answer.Value,
                             CorrectAnswer = Convert.ToInt32(answer.Attribute("value").Value)
                         }).ToList()
                  }).ToList();
于 2012-08-20T16:33:21.790 回答
0

这应该工作

var quizQuestions = (from question in data.Descendants("question")
                  select new QuizQuestion
                  {

                    //...                    

                    Answers = (from answer in question.Descendants("answer")
                              select new QuizAnswer
                              {

                                 //...

                              }).ToList(),

                  }).ToList();
于 2012-08-20T15:52:02.217 回答
0

像这样的东西:

IEnumerable<QuizQuestion> questions =
    from step in doc.Descendants("step")
    from question in step.Descendants("question")
    select new QuizQuestion
    {
        StepId = int.Parse(step.Attribute("id").Value),
        QuestionId =  int.Parse(question.Attribute("id").Value),
        QuestionText = question.Element("text").Value,
        Answers = (from answer in question.Descendants("answer")
                    select new QuizAnswer
                    {
                        AnswerId = int.Parse(answer.Attribute("id").Value),
                        CorrectAnswer = int.Parse(answer.Attribute("value").Value),
                        AnswerText = answer.Value
                    }).ToList()
    };
于 2012-08-20T16:15:50.077 回答
0

如果您的 XML 不打算改变结构,您可以从 Schema 生成一个类。在 Visual Studio 工具中使用 xsd.exe,然后使用 XmlSerializer 类将 XML 反序列化到您的类中。

关于如何使用 XSD 工具的 MSDN 文章

关于 XMLSerializer 的 MSDN 文章

于 2012-08-20T16:10:08.427 回答
0

只需对有效的 xml 添加一些检查(某些元素可以为 null 或其他内容)

quizQuestions = (from s in data.Root.Elements("step")
                     from q in s.Elements("question") 
                     where q != null
    orderby s.Attribute("question")
    select new QuizQuestion()
    {
        StepId = Convert.ToInt32(s.Attribute("id").Value),
        QuestionId = Convert.ToInt32(q.Attribute("id").Value),
        QuestionText = q.Value,
        Answers = (from a in q.Element("answers").Elements("answer")
                   select new QuizAnswer()
                       {
                           AnswerId = Convert.ToInt32(a.Attribute("id").Value),
                           CorrectAnswer = Convert.ToInt32(a.Attribute("value").Value),
                           AnswerText = a.Value
                       }).ToList()
    }).ToList();
于 2012-08-20T16:09:22.543 回答