Micha agus Micha agus - 1 year ago 117
C# Question

How to select xmlnode from its key using XPATH

Here is my XML file

<Data0 Key="1" Name="AA" />
<Data1 Key="2" Name="BB" />
<Data2 Key="4" Name="CC" />
<Data3 Key="10" Name="WW" />
<Data4 Key="11" Name="WE" />
<Data5 Key="12" Name="VW" />

I want to select all nodes those their [Key] start with "1" using C# and XML XPATH

I tried this XmlNodeList FKNodes = node.SelectNodes("/ [@Key like '1*']")

Answer Source

You can use * to select element of any name, and use starts-with() function to match the Key attribute value partially :

XmlNodeList FKNodes = node.SelectNodes("//*[starts-with(@Key, '1')]")

xpath demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download