I am working in c# ASP.NET environment. I am trying to deserialise the following XML element into a c# object/class. The element is 3 levels deep.
<Availability>
<RecommendedSegment>
<Duration>1720</Duration>
<FareBasis>Y77OW</FareBasis>
<FlightSegment>
<DepDate>11 August</DepDate>
<DepTime>0830</DepTime>
<ArrDate>11 August</ArrDate>
<ArrTime>1110</ArrTime>
<DepDay>Mon</DepDay>
<ArrDay>Mon</ArrDay>
<DepAirport>LHR</DepAirport>
<DepAirportName>Heathrow</DepAirportName>
<DepCityName>London</DepCityName>
<ArrAirport>FRA</ArrAirport>
<ArrAirportName>Frankfurt Int'l</ArrAirportName>
<ArrCityName>Frankfurt</ArrCityName>
<DepCountry>United Kingdom</DepCountry>
<ArrCountry>Germany</ArrCountry>
<Airline>LH</Airline>
<AirName>Lufthansa</AirName>
<FlightNo>925</FlightNo>
<BookingClass>Y</BookingClass>
<AirCraftType>32A</AirCraftType>
<ETicket>Y</ETicket>
<NonStop>0</NonStop>
<DepTer>1</DepTer>
<ArrTer>1</ArrTer>
<AdtFareBasis>Y77OW</AdtFareBasis>
<ChdFareBasis>
</ChdFareBasis>
<InfFareBasis>
</InfFareBasis>
</FlightSegment>
<FlightSegment>
<DepDate>11 August</DepDate>
<DepTime>1330</DepTime>
<ArrDate>12 August</ArrDate>
<ArrTime>0100</ArrTime>
<DepDay>Mon</DepDay>
<ArrDay>Tue</ArrDay>
<DepAirport>FRA</DepAirport>
<DepAirportName>Frankfurt Int'l</DepAirportName>
<DepCityName>Frankfurt</DepCityName>
<ArrAirport>BOM</ArrAirport>
<ArrAirportName>Bombay</ArrAirportName>
<ArrCityName>Mumbai</ArrCityName>
<DepCountry>Germany</DepCountry>
<ArrCountry>India</ArrCountry>
<Airline>LH</Airline>
<AirName>Lufthansa</AirName>
<FlightNo>756</FlightNo>
<BookingClass>Y</BookingClass>
<AirCraftType>744</AirCraftType>
<ETicket>Y</ETicket>
<NonStop>0</NonStop>
<DepTer>1</DepTer>
<ArrTer>2</ArrTer>
<AdtFareBasis>Y77OW</AdtFareBasis>
<ChdFareBasis>
</ChdFareBasis>
<InfFareBasis>
</InfFareBasis>
</FlightSegment>
<FlightSegment>
<DepDate>12 August</DepDate>
<DepTime>0515</DepTime>
<ArrDate>12 August</ArrDate>
<ArrTime>0620</ArrTime>
<DepDay>Tue</DepDay>
<ArrDay>Tue</ArrDay>
<DepAirport>BOM</DepAirport>
<DepAirportName>Bombay</DepAirportName>
<DepCityName>Mumbai</DepCityName>
<ArrAirport>GOI</ArrAirport>
<ArrAirportName>Dabolim</ArrAirportName>
<ArrCityName>Goa</ArrCityName>
<DepCountry>India</DepCountry>
<ArrCountry>India</ArrCountry>
<Airline>AI</Airline>
<AirName>Air India</AirName>
<FlightNo>984</FlightNo>
<BookingClass>Y</BookingClass>
<AirCraftType>321</AirCraftType>
<ETicket>Y</ETicket>
<NonStop>0</NonStop>
<DepTer>2</DepTer>
<ArrTer>
</ArrTer>
<AdtFareBasis>Y</AdtFareBasis>
<ChdFareBasis>
</ChdFareBasis>
<InfFareBasis>
</InfFareBasis>
</FlightSegment>
<DepAirport>LHR</DepAirport>
<DepCity>LON</DepCity>
<DepCountry>GB</DepCountry>
<DepZone>1</DepZone>
<ArrAirport>GOI</ArrAirport>
<ArrCity>GOI</ArrCity>
<ArrCountry>IN</ArrCountry>
<ArrZone>5</ArrZone>
</RecommendedSegment>
</Availability>
I am trying to capture the data in the following object which is also 3 levels deep;
[DataContract(Name = "Availability")]
public class Availability
{
[DataMember(Name = "RecommendedSegment", Order = 0)]
public RecommendedSegment RecommendedSegment;
[DataMember(Name = "RecommendedSegment", Order = 1)]
public RecommendedSegmentFlights RecommendedSegmentFlights;
}
When I run the code I get the following exception;
"ExceptionMessage":"Type 'Availability' contains two members 'RecommendedSegment' 'and 'RecommendedSegmentFlights' with the same data member name 'RecommendedSegment'. Multiple members with the same name in one type are not supported. Consider changing one of the member names using DataMemberAttribute attribute."
I have learnt that won't work using DataContract. Is there a way to capture the "FlightSegment" elements into the RecommendedSegmentFlights as a list? I have read I need to override the default serialisation behaviour by using either of the following
[OnDeserialized]
void OnDeserialized(StreamingContext c)
{
if (MyCustonObj == null)
{
MyCustonObj = new MyCustomClass();
MyCustonObj.MyStrData = "Overridden in serialization";
}
}
[OnDeserializing]
void OnDeserializing(StreamingContext c)
{
if (MyCustonObj == null)
{
MyCustonObj = new MyCustomClass();
MyCustonObj.MyStrData = "Overridden in deserializing";
}
}
[OnSerialized]
void OnSerialized(StreamingContext c)
{
// if you wan to do somehing when serialized here or just remove them
}
[OnSerializing]
void OnSerializing(StreamingContext c)
{
// if you wan to do somehing during serializing here or just remove them
}
How do I that? All I want is the property RecommendedSegmentFlights in the class to contain a list of FlightSegment elements. I also don't mind if it is a child of RecommendedSegment. For the RecommendedSegmentFlights property I have this;
public class RecommendedSegmentFlights : List<FlightSegmentStructure> { }
Thanks.