2017-12-25 12 views
0

3 개의 다른 객체를 3 개의 다른 XML로 직렬화해야합니다. 또한, 그것을 deserialize 것입니다.클래스 직렬화 디자인

내 클래스 디자인 :

class Program 
{ 
    public static void Main() 
    { 
     CarsPersistence cars = new CarsPersistence() 
     { 
      Cars = new Car[] 
      { 
       new Car{ Name = "Car1", Speed = 201}, 
       new Car{ Name = "Car2", Speed = 202}, 
       new Car{ Name = "Car3", Speed = 203}, 
       new Car{ Name = "Car4", Speed = 204}, 
       new Car{ Name = "Car5", Speed = 205}, 
      } 
     }; 

     WaysPersistence ways = new WaysPersistence() 
     { 
      Ways = new Way[] 
      { 
       new Way{Number = 100, Length = 200 }, 
       new Way{Number = 101, Length = 201 }, 
       new Way{Number = 102, Length = 202 }, 
       new Way{Number = 103, Length = 203 }, 
       new Way{Number = 104, Length = 204 }, 
       new Way{Number = 105, Length = 205 }, 
       new Way{Number = 106, Length = 206 }, 
       new Way{Number = 107, Length = 207 }, 
      } 
     }; 

     CarsWaysPersistence carsWays = new CarsWaysPersistence() 
     { 
      CarsWays = new KeyValuePair<Car, Way>[] 
      { 
       new KeyValuePair<Car, Way>(cars.Cars[0], ways.Ways[2]), 
       new KeyValuePair<Car, Way>(cars.Cars[3], ways.Ways[1]) 
      } 
     }; 
    } 
} 

[Serializable] 
public class Car 
{ 
    public int Speed { get; set; } 
    public string Name { get; set; } 

} 

[Serializable] 
public class CarsPersistence 
{ 
    public Car[] Cars { get; set; } 
} 

[Serializable] 
public class Way 
{ 
    public int Number { get; set; } 
    public int Length { get; set; } 
} 

[Serializable] 
public class WaysPersistence 
{ 
    public Way[] Ways { get; set; } 
} 

public class CarsWaysPersistence 
{ 
    public KeyValuePair<Car, Way>[] CarsWays { get; set; } 
} 

나는 다른 XML 파일에 3 개 지속성 클래스 (CarsPersistence, WaysPersistence, CarsWaysPersistence)를 직렬화합니다.

내 문제는 중복입니다. CarsWaysPersistence에는 이미 다른 XML 파일로 구성된 Car 및 Way 개체가 있습니다. 아마도 XML 파일에 참조를 저장할 수 없습니다.

이 복제를 어떻게 해결할 수 있습니까? 감사합니다.

+0

대신 다음을 사용하십시오. public KeyValuePair [] CarsWays {get; 세트; } : 공공 사전 > CarsWays {get; 세트; } – jdweng

+0

jdweng 감사하지만 도움이되지 않습니다 – zzfima

+0

목록 중복 문제를 해결해야합니다. – jdweng

답변

1

1) 모든 차량을 일련 번호로 지정하십시오. 2) 모든 방법을 직렬화하십시오. 3) 차의 Name고유 한 식별자로 사용하십시오. Way에도 고유 한 키를 제공해야합니다. 이렇게하면 키 (<car_name, way_key>)로 매핑 할 수 있습니다.