我在下面的链接中获得了序列化课程的答案
XML de-serialization using xml element/attributes
但是我必须包括所有元素,而不管它们在XML中的位置。
XML:
<form>
<question id="QnA">
<answer>AnswerforA</answer>
</question>
<question id="QnB">
<answer>AnswerforB</answer>
</question>
<question id="QnC">
<answer>AnswerforC1</answer>
</question>
<section>
<question id="Qnd">
<answer>Answerford</answer>
</question>
</section>
</form>
的.cs:
[XmlRoot("form")]
public class Form
{
[XmlElement("question")]
public List<Question> Questions { get; set; }
public Form()
{
Questions = new List<Question>();
}
}
public struct Question
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlElement("answer")]
public string Answer { get; set; }
}
在此,我可以在问题列表中获得三个元素QnA,QnB,QnC。
如何指定XML元素,使其包含所有问题元素,即在列表中也包含QnD元素。
谢谢
答案 0 :(得分:0)
将此课程添加到您的项目中:
public class Section
{
[XmlElement("question")]
public List<Question> Questions {get; set;}
public Section()
{
Questions = new List<Question>();
}
}
像这样修改Form
类:
[XmlRoot("form")]
public class Form
{
[XmlElement("question")]
public List<Question> Questions { get; set; }
[XmlElement("section")]
public List<Section> Sections {get; set;}
public Form()
{
Questions = new List<Question>();
Sections = new List<Section>();
}
}