在实现C#导出List数据到xml文件[附源码]时遇到错误:因其保护级别而不可访问。只能处理公共类型。
产生错误的代码行:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<UserModel>));错误原因:以下代码中类定义为:
class UserModel没有添加public访问修饰符
/// <summary> /// 用户实体类 /// /// 注意:类的访问修饰符必须是:public,否则会出现错误: /// 因其保护级别而不可访问。只能处理公共类型。 /// </summary> [Serializable] public class UserModel { private long id = 1; private string name = "name"; private int gender = 0; private int age = 20; private string userName = "userName"; private string password = "password"; public long Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public int Gender { get { return gender; } set { gender = value; } } public int Age { get { return age; } set { age = value; } } public string UserName { get { return userName; } set { userName = value; } } public string Password { get { return password; } set { password = value; } } }