我有一个名为 Questions 的自定义属性,代码如下。
public class Questions
{
private List<Question> _q = new List<Question>();
public List<Question> Question
{
get { return _q; }
}
}
public class Question
{
public string Text { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Answer { get; set; }
private List<string> _Options = new List<string>();
public List<string> Option {
get { return _Options; }
}
}
然后我用下面的代码填充属性
XmlDocument doc = new XmlDocument();
Question q = new Question();
Questions qs = new Questions();
doc.Load(string.Format(@"questions.xml"));
XmlNodeList list = doc.SelectNodes("/questions/question");
foreach (XmlNode node in list)
{
q.Text = node.SelectSingleNode("text").InnerText;
q.Type = node.SelectSingleNode("type").InnerText;
q.Name = node.SelectSingleNode("name").InnerText;
XmlNodeList options = doc.SelectNodes("/questions/question/options");
foreach (XmlNode option in options)
{
q.Option.Add(option.SelectSingleNode("option").InnerText);
}
load.Visible = false;
qa.Visible = true;
qs.Question.Add(q);
DisplayQuestion(qs);
}
现在,当我尝试使用下面的代码访问它时,我没有得到我期望的输出。因此,这就是我需要帮助的地方。示例 XML 位于底部
Label1.Text = q.Question[CurrentQ].Text;
for (int i = 0; i < q.Question[CurrentQ].Option.Count; i++)
{
CheckBoxList1.Items.Add(q.Question[CurrentQ].Option[i]);
}
XML:
<?xml version="1.0"?>
<questions>
<question>
<num>1</num>
<type>radio</type>
<text>Do you like cake?</text>
<options>
<option>Yes</option>
<option>No</option>
<option>Sometimes</option>
</options>
<name>cake</name>
</question>
<question>
<num>2</num>
<type>dropdown</type>
<text>Do you like TV?</text>
<options>
<option>Yes</option>
<option>No</option>
<option>Sometimes</option>
</options>
<name>tv</name>
</question>
<question>
<num>3</num>
<type>checkbox</type>
<text>What do you like?</text>
<options>
<option>Cake</option>
<option>TV</option>
<option>Flipper Reruns</option>
</options>
<name>flipper</name>
</question>
</questions>
和输出:
你喜欢什么?
是的
是的
蛋糕
是的
是的
蛋糕
是的
是的
蛋糕