Does XPath select elements with a specific attribute value?

I'm having trouble choosing nodes using XPath. I will show an example, the xml file is shrinking due to the large amount of data in real:

This is a subset of XML:

<?xml version="1.0" encoding="ISO-8859-1"?> <design xmlns="namespace_hidden" createddate="2012-12-07" createdby="User" name="New Design"> ... <variables> <measurements> <measurement name="Measurement001"> <sample name="1"> <position>[0,0]</position> <variables> <qualitative name="bId"> <class>2193</class> </qualitative> </variables> </sample> ... <sample name="4"> <position>[3,0]</position> <variables> <qualitative name="Q2"> <class>V0</class> </qualitative> <qualitative name="Q3"> <class>V2</class> </qualitative> <qualitative name="Q4"> <class>V1</class> </qualitative> <quantitative name="Q5"> <unit>Percent</unit> <value>8</value> </quantitative> </variables> </sample> </measurement> <measurement name="Measurement002"> .. </measurement> ... </measurements> </design> 

Now im trying to select all the variables under a specific pattern under a specific dimension.

This is the request method that I use:

 // Creating the navigator var doc = new XPathDocument(xmlDoc[0]); var navigator = doc.CreateNavigator(); // Creating the namespace manager: XmlNamespaceManager nsMan = null; if (navigator.NameTable != null) { nsMan = new XmlNamespaceManager(navigator.NameTable); nsMan.AddNamespace("y", xmlNs); nsMan.PushScope(); } // Executing the query var iterator = navigator.Select(string.Format("/y:design/y:measurements/y:measurement[name='{0}']/y:sample[name={1}]/y:variables/y:qualitative", currentMeasurement.Name, currentSample.Name), nsMan); 

When I use this query, I get the first dimension and the first sample, so it works:

 string.Format("/y:design/y:measurements/y:measurement[1]/y:sample[1]" 

but if I use this query:

 "/y:design/y:measurements/y:measurement[name='Measurement001']/y:sample[1]" 

I get no results

In desperation, I also tried different combinations of β€œaround” attribute values ​​without success.

What am I doing wrong?

Best regards, thanks for any help! Richard

+4
source share
1 answer

Use the at @ sign to access attributes:

 /y:design/y:measurements/y:measurement[@name='Measurement001']/y:sample[1] ^ 
+6
source

All Articles