0

我有以下xml结构

<quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="quiz.xsd">
    <mchoice>
        <question>What is the capital city of Australia?</question>
        <answer>Sydney</answer>
        <answer correct="yes">Canberra</answer>
        <answer>Melbourne</answer>
        <answer>Gold Coast</answer>
    </mchoice>
</quiz>

我想使用以下代码将其转换为 CLR 类型

public class Question
{
    public int ID { get; set; }
    public string QuestionText { get; set; }
    public List<Answer> Answers { get; set; }
}

public class Answer
{
    public string Answer1 { get; set; }
    public string Answer2 { get; set; }
    public string Answer3 { get; set; }
    public string Answer4 { get; set; }

}




public List<Question> GetAll()
        {
            var doc = XDocument.Load(Filepath);
            var results = (from x in doc.Descendants("mchoice")
                           select new Question()
                                      {
                                          ID = Convert.ToInt16(x.Element("ID")),
                                          QuestionText = x.Element("question").Value.ToString(),
                                          Answers = new List<Answer>()
                                          {
                                             //How I would fill Answer collection

                                          }


                                      }).ToList();
            return results;
        }

现在我对当前的设计有两个问题。

  1. 我将如何填写答案集合
  2. 我将如何处理当前设计中的正确答案属性。
4

2 回答 2

1

看起来您的模型不匹配。类中有四个单独的“答案”属性Answer。但是,您的Question类包含一个Answer对象列表。并且 XML 包含对应于单个问题的四个答案。似乎更准确的问题模型看起来像:

public class Question
{
    public int ID { get; set; }
    public string QuestionText { get; set; }
    public List<string> Answers { get; set; }
    public int CorrectAnswerIndex { get; set; } 
}

在这种情况下,Answer可以完全忽略该类,

或者,您可以保持Question课程不变并将Answer课程更改为看起来更像

public class  Answer
{
    public string AnswerText { get; set; }
    public bool IsCorrect { get; set; }
}

如果您使用这两种方法中的任何一种,就会更容易了解如何进行 XML 映射。但是,正如您提出的问题,没有正确的答案,因为设计存在根本缺陷

对于第一个示例,您可以按照@LB 的建议进行 XML-Object 映射。但是,您需要将该CorrectAnswerIndex属性设置为等于列表中正确答案字符串的索引。

于 2012-09-20T15:13:43.393 回答
1

有第一个想法

    public class Question
    {
        public int ID { get; set; }
        public string QuestionText { get; set; }
        public Answer Answer { get; set; }
    }

    public class Answer
    {
        public string Answer1 { get; set; }
        public string Answer2 { get; set; }
        public string Answer3 { get; set; }
        public string Answer4 { get; set; }

    }
var results = (from x in doc.Descendants("mchoice")
               let answers = x.Elements("answer")
               select new Question()
                 {
                      ID = Convert.ToInt16(x.Element("ID")),
                      QuestionText = x.Element("question").Value,
                      Answer = new Answer
                        {
                            Answer1 = answers.First().Value,
                            Answer2 = answers.Skip(1).First().Value,
                            Answer3 = answers.Skip(2).First().Value,
                            Answer4 = answers.Last().Value
                         }


                  }).ToList();

使用更简单的模型

public class Question
    {
        public int ID { get; set; }
        public string QuestionText { get; set; }
        public List<string> AnswerTextList { get; set; }
    }

var results = (from x in doc.Descendants("mchoice")

                           select new Question()
                           {
                               ID = Convert.ToInt16(x.Element("ID")),
                               QuestionText = x.Element("question").Value,
                               AnswerTextList = (x.Elements("answer").Select(m => m.Value.ToString()).ToList())
                           }).ToList();

有一个好的模型

public class Question
    {
        public int ID { get; set; }
        public string QuestionText { get; set; }
        public List<Answer> AnswerList { get; set; }
    }

    public class Answer
    {
        public string Text { get; set; }
        public bool IsCorrect { get; set; }
    }

var results = (from x in doc.Descendants("mchoice")
        select new Question()
        {
            ID = Convert.ToInt16(x.Element("ID")),
            QuestionText = x.Element("question").Value,
            AnswerList = (x.Elements("answer").Select(m => new Answer
            {
                Text = m.Value,
                IsCorrect = m.Attribute("correct") != null && m.Attribute("correct").Value == "yes"
            }).ToList())
        }).ToList();
于 2012-09-20T15:18:48.583 回答