목록 데이터 멤버와 클래스를 직렬화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는 표시되지 않습니다.
- 어떻게 해결할 수 있습니까? 나는 그 목록을 재산으로 만들었지 만, 여전히 효과가없는 것 같습니다.
- 여기에 xml serialization을 사용하고 있습니다. 다른 serializer가 있습니까?
- 성능을 원합니다! 이것이 최선의 접근 방법입니까?
--------------- 업데이트 아래 -------------------------
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
아마도'XmlSerializer'는'IList <>'에 문제가 있습니다. 대신에'List <>'로 재정의하면 어떻게 될까요? – Nate