我的xml是这样的:
<Destination>
<CityCode>GRAGMRNCHN</CityCode>
<CityName>Agia Marina</CityName>
<ZoneCode>CRTCHN</ZoneCode>
<ZoneName>Creta - Chania</ZoneName>
<Departures>
<Departure>
<CountryCode>RO</CountryCode>
<CountryName>Romania</CountryName>
<CityCode>ROBCH1</CityCode>
<CityName>Bucuresti</CityName>
<GiataCodes>
<GiataCode>28029</GiataCode>
<GiataCode>759</GiataCode>
<GiataCode>28575</GiataCode>
<GiataCode>756</GiataCode>
<GiataCode>47839</GiataCode>
</GiataCodes>
<Dates>
<Date TourOpCode="EU" Nights="7,14,21">2016-06-22</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-06-29</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-07-06</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-07-13</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-07-20</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-07-27</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-08-03</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-08-10</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-08-17</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-08-24</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-08-31</Date>
<Date TourOpCode="EU" Nights="7,14,21">2016-09-07</Date>
<Date TourOpCode="EU" Nights="7,14">2016-09-14</Date>
<Date TourOpCode="EU" Nights="7">2016-09-21</Date>
</Dates>
</Departure>
现在我要提取CityCode(目的地),CityCode(出发),可用日期和他们的夜晚属性。我在约会时堆叠!使用Xdcument我只得到日期的第一个元素。
这是解析代码:
packageList = (from destination in xDoc.Descendants(nonamespace + "Destination")
select new PackageInformation
{
DestinationCityCode = destination.Element("CityCode").Value,
DepartureCityCode = destination.Descendants("Departure")
.Select( c => new DepartureCityClass()
{
DepartureCityCode = c.Element("CityCode").Value
}).ToList(),
Dates = destination.Descendants("Departures").Descendants("Departure").Descendants("Dates")
.Select(d => new DatesClass()
{
Date = d.Element("Date").Value,
Nights = d.Element("Date").Attributes("Nights").First().Value
}).ToList()
}).ToList();
任何帮助! 感谢。
答案 0 :(得分:1)
尝试这个,应该可以正常工作:
var packageList = (from destination in xd.Descendants("Destination")
select new
{
DestinationCityCode = destination.Element("CityCode").Value,
DepartureCityCode = destination.Descendants("Departure")
.Select(c => new
{
DepartureCityCode = c.Element("CityCode").Value
}).ToList(),
Dates = destination.Descendants("Departure").Descendants("Dates").Descendants("Date")
.Select(d => new
{
Date = d.Value,
Nights = d.Attributes("Nights").First().Value
}).ToList()
}).ToList();