1

My xml is like this:

<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>

Now i want to extract CityCode(destination), CityCode(departure), available date and their nights attributes. I'm stack at dates! Using Xdcument i get only first elements from date.

Here is parsing code:

 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();

Any help! Thanks.

5
  • It looks like you miss .Descendants("Date") after .Descendants("Dates"). Commented Mar 16, 2016 at 15:19
  • If I am using .Descendants("Date") after Descendants("Dates") then throws an error: Object reference not set to an instance of an object Commented Mar 16, 2016 at 15:24
  • You should use d.Value and d.Attributes("Nights") instead of d.Element("Date").Value and d.Element("Date").Attributes("Nights") Commented Mar 16, 2016 at 15:26
  • I agree. Plus, in many of the cases it's not necessary to use .Descendants(), as this will also search all child of child of child of... elements, which decreases performance. As you're only looking at direct children anyway, use .Element() instead. Commented Mar 16, 2016 at 15:28
  • I got the idea. Thank's a lot ! Commented Mar 16, 2016 at 15:38

1 Answer 1

1

Try this one, should work fine:

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.