0

From the XML below how can we extract values of speed 'key values' based on the given deliveryFormat. e.g. The speed values for deliveryFormat key=1 are 2,3,4 and for deliveryFormat key=4 are 5,6,4

The following code gives me the keys for the availableFormats and based on these keys I want to extract the speed key values

XmlDocument results = new XmlDocument();
results.LoadXml(theModel.SearchLog.AvailabilityXML);
var AvailableFormats = results.SelectNodes("//apiResponse/availableFormats/availableFormat/deliveryFormat/@key");

XML

<?xml version="1.0" encoding="UTF-8"?>
<apiResponse>
<availableFormats>
    <availableFormat availabilityDate="2014-01-31">
        <deliveryFormat key="1">Encrypted Download</deliveryFormat>
        <deliveryModifiers/>
        <availableSpeeds>
            <speed key="2">2 Hours</speed>
            <speed key="3">24 Hours</speed>
            <speed key="4">4 Days</speed>
        </availableSpeeds>
        <availableQuality>
            <quality key="1">Standard</quality>
            <quality key="2">High</quality>
        </availableQuality>
    </availableFormat>
    <availableFormat availabilityDate="2014-01-31">
        <deliveryFormat key="4">Paper</deliveryFormat>
        <deliveryModifiers/>
        <availableSpeeds>
            <speed key="5">2 Hours</speed>
            <speed key="6">24 Hours</speed>
            <speed key="4">4 Days</speed>
        </availableSpeeds>
        <availableQuality>
            <quality key="1">Standard</quality>
            <quality key="2">High</quality>
        </availableQuality>
    </availableFormat>
</availableFormats>
</apiResponse>
rumi
  • 3,293
  • 12
  • 68
  • 109

1 Answers1

1

Please try the following xpath. It gives you all speed for deliveryFormat 1. You can change the key value as needed.

//availableFormat[deliveryFormat/@key='1']//speed

Or, if you want just the speed keys:

//availableFormat[deliveryFormat/@key='1']//speed/@key

EDIT: Fixed xpath conditional

Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
  • Awesome @wdosanjos. I appreciate your answer but don't really understand these guys who are marking this question un-useful or duplicate. – rumi Feb 13 '14 at 12:35