WCF/WF - WCF Data Service With relationships - Asked By anil babu on 07-Jul-14 01:41 PM

namespace WcfrestService
{
    [DataContract]
    public class Employee
    {
        [DataMember]
        public int EmpNo { get; set; }

        [DataMember]
        public string EmpName { get; set; }
        public List<Department> AllDepart { get; set; }
        [DataMember]
        public string DeptName { get; set; }
        
    }

    [DataContract]
    public class Department
    {
        [DataMember]
        public int DeptNo { get; set; }

        [DataMember]
        public string Dname { get; set; }

        [DataMember]
        public string Location { get; set; }      

        public Employee Employee { get; set; }
    }
    public partial class EmployeeData
    {
        private static readonly EmployeeData _instance = new EmployeeData();

        private EmployeeData() { }

        public static EmployeeData Instance
        {
            get
            {
                return _instance;
            }
        }
        List<Employee> empList = new List<Employee>
        {
                
                new Employee() { EmpNo=101,EmpName="Anil",DeptName="DEV", 
                    AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"},
                                                     new Department (){ DeptNo=2,Dname="DEv",Location="Gun"},
                                                     new Department (){ DeptNo=3,Dname="pro",Location="Ong"}
                    }}
        };
        public List<Employee> EmployeeList
        {
            get
            {
                return empList;
            }
        }
        public void Update(Employee updEmployee)
        {
            Employee existing = empList.Find(p => p.EmpNo == updEmployee.EmpNo);

            if (existing == null)
                throw new KeyNotFoundException("Specified Employee cannot be found");

            existing.EmpName = updEmployee.EmpName;
            existing.DeptName = updEmployee.DeptName;
            //existing.Age = updEmployee.Age;
        }

        public void Add(Employee newEmployee)
        {
            empList.Add(new Employee
            {
                EmpNo = newEmployee.EmpNo,
                EmpName = newEmployee.EmpName,
                DeptName = newEmployee.DeptName,

            });
        }

        public void Delete(int empid)
        {
            Employee existing = empList.Find(p => p.EmpNo == empid);
            empList.Remove(existing);
        }
    }
}
[ServiceContract]
    public interface IService
    {
        [WebGet(UriTemplate = "Employee", ResponseFormat = WebMessageFormat.Xml)]
        [OperationContract]
        List<Employee> GetAllEmployeeDetails();
}
Implementation service

 public List<Employee> GetAllEmployeeDetails()
        {
            return EmployeeData.Instance.EmployeeList;
        }

I am trying to display data from both the class but it's getting only single class only

 

I want to out putlike below

 

<Employee>

<EmpNo>1</EmpNo>

<Ename>Anil</Ename>

    <Department>

        <DeptNo>

      <DeptNo>10</DeptNo>

      <Dname>Sales</Dname>

      <Location>Vij</Location>

  </Department>

<DeptName>DEV</DeptName>

</Employee>

I want to above output,

How can i get that type of output?