0
我找到了handling circular reference when using xml serialization的解決方案。但是,在我來說,我有一個列表:使用XML序列化的循環引用
public class Category
{
public Category()
{
Items = new List<CategoryItem>();
}
public string CategoryName { get; set; }
public List<CategoryItem> Items { get; set; }
}
和:
public class CategoryItem
{
public string Link { get; set; }
public Category Category { get; set; }
}
計劃:
private static void Main(string[] args)
{
var programmingCategory = new Category {CategoryName = "Programming"};
var ciProgramming = new CategoryItem
{
Link = "www.stackoverflow.com",
Category = programmingCategory
};
var fooCategory = new CategoryItem
{
Category = programmingCategory,
Link = "www.foo.com"
};
programmingCategory.Items.Add(ciProgramming);
programmingCategory.Items.Add(fooCategory);
var serializer = new XmlSerializer(typeof (Category));
var file = new FileStream(FILENAME, FileMode.Create);
serializer.Serialize(file, programmingCategory);
file.Close();
}
我總是得到一個
出現InvalidOperationException
我該如何解決這個問題?
您的類必須具有[Serializable]屬性才能使XmlSerializer開心。什麼是實際的信息? –
@AloisKraus IIRC'XmlSerializer'不需要'[Serializable]'屬性 –
Hm可能會有助於獲取異常消息。因爲有序列化器要求它,添加這個屬性仍然是一個好主意。 –