基本上我已经创建了一个用户界面,用户输入存储在两个列表中。
一个类包含两个列表,我试图XMLSerializer
通过仅向序列化程序传递类名来编写一个包含正确嵌套的列表的 XML 文件。
每次我尝试这样做时,都会出现异常:
InvalidOperationException,生成 XML 文档时出错
我怎样才能解决这个问题?
代码...
//declared globally
className mInfo = new className(); //create an object that references the class
classTwo aInfo = new assessments(); //used in a seperate method in UI ignore for now.
//end of objects
//start of class - assume is has a constructor for now
[Serializable()]
[XmlRoot]
public class className
{
public string mCode;
public string mTitle;
public int nOC;
public string term;
public string oModM;
public bool hExam;
//create a list to hold the data
[XmlElement]
public List<className> mList = new List<className>();
[XmlElement]
public List<classTwo> aList = new List<classTwo>();
//end of class
//start of writer...
public void XMLWriter()
{
string filePath = "test.xml";
// THIS PART CREATES THE XML FILE
XmlSerializer serializer = new XmlSerializer(typeof(className));
TextWriter textWriter = new StreamWriter(filePath);
serializer.Serialize(textWriter, mInfo);
textWriter.Close();
}
//end of writer