Tell me how it is possible to write all xpath results into an array?

I have the following XML:

<cspr:CharacteristicValueList> <cspr:name>member</cspr:name> <cspr:CharacteristicValue> <gen:name>id</gen:name> <gen:value>sip:+00112233111@qwerty.qq</gen:value> </cspr:CharacteristicValue> </cspr:CharacteristicValueList> <cspr:CharacteristicValueList> <cspr:name>member</cspr:name> <cspr:CharacteristicValue> <gen:name>id</gen:name> <gen:value>sip:+00112233222@qwerty.qq</gen:value> </cspr:CharacteristicValue> </cspr:CharacteristicValueList> <cspr:CharacteristicValueList> <cspr:name>member</cspr:name> <cspr:CharacteristicValue> <gen:name>id</gen:name> <gen:value>sip:+00112233333@qwerty.qq</gen:value> </cspr:CharacteristicValue> </cspr:CharacteristicValueList> 

I want to pull out all 3 values ​​from gen: value For this, I wrote Xpath, which finds all these values, but I don’t understand how to derive them all. Below is my code + Xpath, which finds all 3 values, but the code displays only the first value: sip: +00112233111@qwerty.qq How can I get all the values? In XML they can be any number. I thought to make a loop (While), but I get an error Object link does not indicate an object instance.

 string act = "/soapenv:Envelope/soapenv:Body/*/csp:compositeService/cspr:serviceSpecificationContainer/*/cspr:name[text() = 'account-cac']/../*/*/cspr:name[text() = 'member']/../*/gen:value"; if (nav.SelectSingleNode(act, nsmgr) != null) { XPathNavigator acts = nav.SelectSingleNode(act, nsmgr); account_cac = acts.Value; } 

Xpath gives 3 values:

 sip:+00112233111@qwerty.qq sip:+00112233222@qwerty.qq sip:+00112233333@qwerty.qq 

It is required to put them in one array, or immediately in a string.

 result = string.Join("|", account_cac_array); 
  • described in more detail. - MystX
  • Instead of SelectSingleNode , use SelectNodes . - Alexander Petrov

1 answer 1

In XPathNavigator there are Select methods that allow you to select a set of nodes using a given expression. They return an XPathNodeIterator iterator.

You can process the results in your usual foreach (or while ) while , but you can use the OfType method.

Thus, to obtain an array, you need approximately the following code:

  string[] arr = nav.Select(act, nsmgr) .OfType<XPathNavigator>() .Select(x => x.Value) .ToArray();