2011-03-30 4 views
1

목록 데이터 멤버와 클래스를 직렬화C#을 내가이 C# 클래스를

 Test t = new Test(); 
     t.list.Add(1); 
     t.list.Add(2); 

     IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication(); 
     StringWriter sw = new StringWriter(); 
     XmlSerializer xml = new XmlSerializer(t.GetType()); 
     xml.Serialize(sw, t); 

나는 자상의 출력을 볼 때, 그이 :

<?xml version="1.0" encoding="utf-16"?> 
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /> 

목록의 멤버 변수에 추가 한 값 1,2는 표시되지 않습니다.

  1. 어떻게 해결할 수 있습니까? 나는 그 목록을 재산으로 만들었지 만, 여전히 효과가없는 것 같습니다.
  2. 여기에 xml serialization을 사용하고 있습니다. 다른 serializer가 있습니까?
  3. 성능을 원합니다! 이것이 최선의 접근 방법입니까?

--------------- 업데이트 아래 -------------------------

public class RoutingResult 
    { 
     public float lengthInMeters { get; set; } 
     public float durationInSeconds { get; set; } 

     public string Name { get; set; } 

     public double travelTime 
     { 
      get 
      { 
       TimeSpan timeSpan = TimeSpan.FromSeconds(durationInSeconds); 
       return timeSpan.TotalMinutes; 
      } 
     } 

     public float totalWalkingDistance 
     { 
      get 
      { 
       float totalWalkingLengthInMeters = 0; 
       foreach (RoutingLeg leg in Legs) 
       { 
        if (leg.type == RoutingLeg.TransportType.Walk) 
        { 
         totalWalkingLengthInMeters += leg.lengthInMeters; 
        } 
       } 

       return (float)(totalWalkingLengthInMeters/1000); 
      } 
     } 

     public IList<RoutingLeg> Legs { get; set; } // this is a property! isnit it? 
     public IList<int> test{get;set;} // test ... 

     public RoutingResult() 
     { 
      Legs = new List<RoutingLeg>(); 
      test = new List<int>(); //test 
      test.Add(1); 
      test.Add(2); 
      Name = new Random().Next().ToString(); // for test 
     } 
    } 

그러나 시리얼 라이저에 의해 생성 된 XML은 이것이다 :

그래서 내가 직렬화 할 실제 클래스는 이것이다

<RoutingResult> 
    <lengthInMeters>9800.118</lengthInMeters> 
    <durationInSeconds>1440</durationInSeconds> 
    <Name>630104750</Name> 
</RoutingResult> 

???

해당 목록을 모두 무시합니까? 귀하의 list

+1

아마도'XmlSerializer'는'IList <>'에 문제가 있습니다. 대신에'List <>'로 재정의하면 어떻게 될까요? – Nate

답변

4

1)는 필드가 아닌 속성, 그리고 XmlSerializer가이 속성 만 작동합니다,이 시도 :

public class Test 
{  
    public Test() { IntList = new List<int>() }  
    public IList<int> IntList { get; set; } 
} 

2)Binary, 주요 다른 Serialiation의 옵션이 있습니다 다른 하나, JSON도 있습니다.

3) 이진수는 일반적으로 스트레이트 메모리 덤프이며 출력 파일이 가장 작기 때문에 아마도 가장 좋은 방법입니다.

+2

또한 IList 에서는 XmlSerializer가 작동하지 않으므로 멤버 변수를 List 으로 변경했습니다. 그런 다음 작동합니다. –

1

list는 (는) 재산이 아닙니다. 공개적으로 볼 수있는 속성으로 변경하면 가져와야합니다.

1

IList를 사용하면 XmlSerializer가 작동하지 않는다는 것을 알아 냈으므로 List로 변경하여 작동하게했습니다. 네이트도 언급했듯이.

관련 문제