user704314
user704314

Reputation: 35

Windows phone 7 xml linq queries trouble

Hi guys I'm developing an app for windows phone 7 and I'm having some trouble parsing an XML document using XDocument and Linq queries. I only want a specific bit of a quite large piece of XML and there are many duplicate names on nodes.

<itemListCollection xmlns="urn:webjet.com.au" xmlns:i="http://www.w3.org/2001/XMLSchema-?Instance">
<itemList>
    <listCode>FlightSearchAirportsTT</listCode>
    <items>
        <item>
            <code>ADL</code>
            <value>Adelaide</value>
        </item>
        <item>
            <code>BNE</code>
            <value>Brisbane</value>
        </item>
         ....
         ....
    </items>
</itemList>
<itemList>
    <listCode>AIRPORTCODES_LATLONS</listCode>
    <items>
        <item>
            <code>EBB</code>
            <value>0.060277777777778;32.4</value>
        </item>
        <item>
            <code>NYK</code>
            <value>0.066666666666667;37.03333333333</value>
        </item>
                    ........
                    .......
  <item>
            <code>KAB</code>
            <value>ZW</value>
        </item>
        <item>
            <code>VFA</code>
            <value>ZW</value>
        </item>
    </items>
</itemList>

I only want the items in the first itemList so I can use them to create an object with two attributes airport code and airport name. However I don't know how to extract that exact part.

Upvotes: 2

Views: 308

Answers (1)

harryovers
harryovers

Reputation: 3138

This will return the first XElement named itemList.

        // if your xml is in a file:
        XDocument doc = XDocument.Load("/path/of/xml");

        // if your xml is in a string field:
        XDocument doc = XDocument.Parse(stringField);
        XElement firstList = doc.Descendants().Where(x => x.Name == "itemList").First();

Upvotes: 4

Related Questions