Search code examples
c#xmlxpathselectsinglenode

XPath string for selecting data types


I have an XML Document that specifies certain data instead data nodes like so:

<data type= "arbitrary value">Value</data>

I need to be able to look through this document and select the above node.

XmlNode node = xmlDoc.SelectSingleNode(data[contains(.,'arbitrary value')]);

The above statement does not work.

How can I find nodes with a certain "type"?


Solution

  • The xpath data[@type='arbitrary value'] will select all "data" nodes with a type attribute containing the text "arbitrary value"

    So:

    XmlNode node = xmlDoc.SelectSingleNode("data[@type='arbitrary value']")