****I serialize my Employee collection on xml file. When i try to deserialize collection from that or another them program throws exception: An unhandled exception of type "System.InvalidOperationException" in System.Xml.dll For more information : The document XML ( 2 , 2 ) there is an error . XML file:
`
<?xml version="1.0" encoding="utf-8"?>
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Employee>
<FirstNameP>Ogirenko</FirstNameP>
<AgeP>19</AgeP>
<DepartmentP>.NET Sharepoint</DepartmentP>
<AddressP>Kharkov</AddressP>
</Employee>
<Employee>
<FirstNameP>Belous</FirstNameP>
<AgeP>19</AgeP>
<DepartmentP>.NET Sharepoint</DepartmentP>
<AddressP>Kharkov</AddressP>
</Employee>
</Items>
</Employees>`
Code:
public class Employee
{
private string FirstName;
private string LastName;
private int Age;
private string Department;
private string Address;
private string EmpoyeeID;
#region Properties
public string FirstNameP
{
get { return FirstName; }
set { FirstName = value; }
}
public string LastNameP
{
get { return LastName; }
set { FirstName = value; }
}
public int AgeP
{
get { return Age; }
set { Age = value; }
}
public string DepartmentP
{
get { return Department; }
set { Department = value; }
}
public string AddressP
{
get { return Address; }
set { Address = value; }
}
#endregion
}
[XmlRoot("Employees")]
public class MyWrapper
{
private List<Employee> items = new List<Employee>();
[XmlElement("Employee")]
public List<Employee> Items { get { return items; } }
}
public static void Main()
{
DeserializeFromXML();
Console.WriteLine();
}
static public void SerializeToXML(MyWrapper list)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
using (TextWriter textWriter = new StreamWriter(@"Employees.xml"))
{
serializer.Serialize(textWriter, list);
textWriter.Close();
}
}
static public void DeserializeFromXML()
{
Employee employees = null;
string path = "test.xml";
XmlRootAttribute root = new XmlRootAttribute();
root.ElementName = "Employee";
XmlSerializer serializer = new XmlSerializer(typeof(List<Employee>));
StreamReader reader = new StreamReader(path);
//Exception on xml(2,2) file
employees = (Employee)serializer.Deserialize(reader);
reader.Close();
}
}
please, help