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>
6
  • 1
    What have you tried so far? What is not working in your code? Please share your code. Commented Feb 13, 2014 at 12:10
  • Search the web or this site for ".NET parse XML". Commented Feb 13, 2014 at 12:11
  • Please read the question again. I have mentioned what I have tried. thanks Commented Feb 13, 2014 at 12:14
  • I know how to parse the xml but don't know how to extract certain attributes based on a condition on another attribute Commented Feb 13, 2014 at 12:15
  • The following code gives me the XML document Commented Feb 13, 2014 at 12:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome @wdosanjos. I appreciate your answer but don't really understand these guys who are marking this question un-useful or duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.