I hope this will solve your problem:
[XmlRoot("Datatable"), Serializable]
public class Datatable
{
[XmlElement("Employees")]
public Employees Employees { get; set; }
}
public class Employees
{
[XmlAttribute("Count")]
public int Count { get; set; }
[XmlElement("Employee")]
public List<employee> Employee { get; set; }
}
public class Employee
{
[XmlAttribute("Code")]
public string Code { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlElement("Address")]
public Address Address { get; set; }
[XmlElement("Contact")]
public Contact Contact { get; set; }
}
public class Address
{
[XmlAttribute("City")]
public string City { get; set; }
[XmlAttribute("Pin")]
public string Pin { get; set; }
[XmlText]
public string AddressValue { get; set; }
}
public class Contact
{
[XmlAttribute("Mob")]
public string Mob { get; set; }
[XmlAttribute("Phone")]
public string Phone { get; set; }
[XmlAttribute("Email")]
public string Email { get; set; }
[XmlText]
public string ContactValue { get; set; }
}
To load the object from the file:
XmlSerializer serializer = new XmlSerializer(typeof(Datatable));
FileStream loadStream = new FileStream("sample.xml", FileMode.Open, FileAccess.Read);
Datatable loadedObject = (Datatable)serializer.Deserialize(loadStream);
loadStream.Close();
and sample.xml need to look like this:
="1.0"
<datatable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<employees count="2">
<employee code="A" name="XYZ">
<address city="A" pin="0000" />
<contact mob="123456" phone="02145235" email="XYZ@domain.com" />
</employee>
<employee code="B" name="ABC">
<address city="C" pin="111" />
<contact mob="78794656" phone="+912546" email="ABC@domain.com" />
</employee>
</employees>
</datatable>