我试图序列化标准员工 - 部门结构
[Serializable]
public class EmployeeBean
{
[XmlElement( "Id" )] public string Id { get; set; }
[XmlElement( "Id" )] public string Name { get; set; }
[XmlElement( "DeparmentName" )] public string EmpDeparment { get; set; }
[XmlElement( "Age" )] public double Age { get; set; }
}
[Serializable]
public class Department
{
[XmlElement( "Id" )] public string Id { get; set; }
[XmlElement( "Name" )] public string Name { get; set; }
[XmlElement( "Description" )] public string Description { get; set; }
[XmlElement( "Employees" )] public List<EmployeeBean> Employees { get; set; }
}
和序列化的列表
private BindingList<EmployeeBean> _empList ;
private BindingList<Department> _dptList ;
XmlSerializer SerializerObj = new XmlSerializer(typeof(Department));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(path);
SerializerObj.Serialize(WriteFileStream, _dptList);
使用序列化它只是在尝试初始化序列化时说错误,而LINQ只是说它创建不均匀。
答案 0 :(得分:1)
不能有两个属性[XmlElement(“Id”)]否则会发生异常。
public class EmployeeBean
{
[XmlElement("Id")]
public string Id
{
get;
set;
}
[XmlElement("Name")]
public string Name
{
get;
set;
}
另一个问题是XmlSerializer(typeof(Department))但_dptList是BindingList 。
//XmlSerializer SerializerObj = new XmlSerializer(typeof(Department));
XmlSerializer SerializerObj = new XmlSerializer(typeof(BindingList<Department>));