2017-02-13 4 views
0
Public Class Employee 
{ 
    Public String EmployeeId {get;set;} 
    Public String EmployeeName {get;set;} 
    Public String Department {get;set;} 
} 

Public Class Department 
{ 
    Public String DepartmentId {get;set;} 
    Public String DepartmentName {get;set;} 
    Public String Address {get;set;} 
} 

Public Class Address 
{ 
    Public String AddrOne {get;set;} 
    Public String City {get;set;} 
} 

나는 목록, 목록 및 목록 채워 얻을해야 3 개 모델, 목록, 목록 및 언급 한 3 개 모델 위의 절차의 목록 후 실행을하고 난 ...리스트 <Model>을 복잡한 응답 유형으로 변환하는 방법?

형식 아래에있는 데이터를 반환해야

아래의 형식으로 응답하는 가장 좋은 방법은 무엇입니까?

<Employees> 
<Employee> 
    <EmployeeID> </EmployeeID> 
    <EmployeeName> </EmployeeName> 
    <Department> 
    <DepartmentID>  </DepartmentID> 
    <DepartmentName>  </DepartmentName> 
    <Address> 
     <Addr1> </Addr1> 
      <City> </City> 
    </Address> 
    <Department> 
</Employee> 
</Employees> 
+1

[XML serialization] (https://msdn.microsoft.com/en-us/library/58a18dwa%28v=vs.110%29.aspx) – Kilazur

+0

@AfnanAhmad, 계층 구조에 따라 3 가지 컬렉션 (직원, 부서 및 주소) 및 xml 구조체를 생성합니다. 내가이 일을 더 나은 방법이 생각 ...하지만 단서 forrach (의 DataRow를 drEmp ...) { xmlElement.Add (다는 EmpID) xmlElement.Add (EmpNAme) forrach을 (이 없습니다 에서의 DataRow를 drEmp ...) { \t xmlElement.Add (DeptID) \t xmlElement.Add (DEPTNAME) \t forrach (DataRow를 drEmp ...) { \t xmlElement.Add (ADDR1) \t } } } – Amit

답변

0

XML-직렬화 :

그것을 직렬화하는 방법을 CollectionClass을 만들고 추가

 MyCollection myCollection = new MyCollection(); 
     //Now add your entries, myCollection.Add(new Department(....)); 

     //Save your class as xml-File 
     File.WriteAllText("C:\\MyClass.xml", myCollection.ToXML()); 

:

public class MyCollection 
{ 
    public List<Employee> = new List<Employee>(); 
    public List<Department> = new List<Department>(); 
    public List<Address> = new List<Address>(); 

    public string ToXML() 
    { 
     var stringwriter = new System.IO.StringWriter(); 
     var serializer = new XmlSerializer(this.GetType()); 
     serializer.Serialize(stringwriter, this); 
     return stringwriter.ToString(); 
    } 

    // You have to use your Class-Type here 3 times 
    public static MyCollection LoadFromXML(string filePath) 
    { 
     using (StreamReader streamReader = new System.IO.StreamReader(filePath)) 
     { 
      var serializer = new XmlSerializer(typeof(MyCollection)); 
      return serializer.Deserialize(streamReader) as MyCollection; 
     } 
    } 
} 

이제 XML 파일로 클래스를 저장할 수 있습니다 그런 다음로드 할 수 있습니다.

 //Load your class 
     MyCollection myCollection = MyCollection.LoadFromXML("C:\\MyClass.xml"); 

편집 : 당신이 모델의 인스턴스를 만들 수 있습니다

public class Model 
{ 
    public List<Employee> Employees { get; set; } 
} 

public class Employee 
{ 
    public string EmployeeId { get; set; } 
    public string EmployeeName { get; set; } 
    public Department Department { get; set; } 
} 

public class Department 
{ 
    public string DepartmentId { get; set; } 
    public string DepartmentName {get; set; } 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    public string AddrOne { get; set; } 
    public string City { get; set; } 
} 

다음과 함께 웁니다 상황

0

다음과 같은 클래스를 만들 수에 맞게해야 CollectionClass-샘플로 변경 데이터로 변환하고 XML로 serialize