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>

この 2 つのクラスを作成しましたが、情報を簡単に読み取る方法がわかりません。私の頭はそれから壊れています。

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

このようなもの:

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にいくつかのチェックを追加するだけです(一部の要素は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 に答える
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

私は私のものをテストしましたが、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 に答える