我有两个如下的类:
public class Info
{
[XmlAttribute] public string language;
public int version;
public Book book;
public Info() { }
public Info(string l, int v, string author, int quantity, int price)
{
this.language = l;
this.version = v;
book = new Book(author, quantity, price);
}
}
public class Book
{
[XmlAttribute] public string author;
public int quantity;
public int price;
[XmlIgnore]public int total;
public NameValueCollection nvcollection = new NameValueCollection();
public Book() { }
public Book(string author, int quantity, int price)
{
this.author = author;
this.quantity = quantity;
this.price = price;
total = quantity * price;
nvcollection.Add(author, price.ToString());
}
}
我创建了一个ArrayList,它添加了两个Info类的实例,如下所示:
FileStream fs = new FileStream("SerializedInfo.XML", FileMode.Create);
List<Info> arrList = new List<Info>();
XmlSerializer xs = new XmlSerializer(typeof(List<Info>));
Info pObj = new Info("ABC", 3, "DEF", 2, 6);
Info pObj1 = new Info("GHI", 4, "JKL", 2, 8);
arrList.Add(pObj);
arrList.Add(pObj1);
xs.Serialize(fs, arrList);
fs.Close();
但是当我尝试进行序列化时,会出现异常,提示信息为 "There was an error reflecting type 'System.Collections.Generic.List`1[ConsoleApplicationSerialization.Info]'."
我该如何调试此问题呢?
另外,除了namevaluecollection,我还能使用哪种类型的结构呢?
[Serializable]
属性对 XML 序列化无关紧要。 - John Saunders