Get an attribute value from XML Response in azure apim

Solution for Get an attribute value from XML Response in azure apim
is Given Below:

I have xml response stored in response-variable-name like:

<B n="1" v="X"/>
<B n="2" v="Y"/>
<B n="3" v="Z"/>
<B n="4" v="XX"/>
<B n="5" v="YY"/>
<B n="6" v="ZZ"/>

I want to parse this xml response and get the value of v where n=3 using apim policy.
Constraint is I can’t convert this response to JSON and then get desired result.

Depending on how exactly namespaces are organized in your XML code below may be simplified a bit, but it should work as is just as well:

<set-variable name="test" value="@(
        .FirstOrDefault(x => x.Name.LocalName == "B" && x.Attributes().FirstOrDefault(a => a.Name.LocalName == "n")?.Value == "3")?
        .FirstOrDefault(a => a.Name.LocalName == "v")?
)" />

Use Xml Linq with a dictionary :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication195
    class Program
        const string FILENAME = @"c:temptest.xml";
        static void Main(string[] args)
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<int, string> dict = doc.Descendants("B")
                .GroupBy(x => (int)x.Attribute("n"), y => (string)y.Attribute("v"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());