I am used to the simple use of xpath. I have studied it when I encounter multiple condition query requirements.
Post the implementation method for your reference:
There is such an XML:
<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?>
<Test>
<Cell> <data type = "string"> alpha </data> </cell>
<Cell> <data type = "Number"> 100 </data> </cell>
<Cell> <data type = "Number"> 200 </data> </cell>
<Cell> <data type = "Boolean"> true </data> </cell>
</Test>
All cell nodes whose data nodes meet the requirement of type = string and inner text = alpha
XPath: // cell [DATA [text () = 'alpha'] and data [@ type = 'string']
Or // cell [DATA [text () = 'alpha' and @ type = 'string']
Analysis: // cell indicates searching all cell nodes
[] It is a condition. Only cell nodes that meet this condition can be searched out.
Data [text () = 'alpha' and @ type = 'string'] subnodes with such (innertext = Alpha and type = string) can be searched out.
Connect multiple conditions with and
What if I add a node?
<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?>
<Test>
<Row>
<Cell> <data type = "string"> alpha </data> </cell>
<Cell> <data type = "Number"> 100 </data> </cell>
<Cell> <data type = "Number"> 200 </data> </cell>
<Cell> <data type = "Boolean"> true </data> </cell>
</Row>
<Row>
<Cell> <data type = "string1"> alpha </data> </cell>
<Cell> <data type = "number1"> 100 </data> </cell>
<Cell> <data type = "number1"> 200 </data> </cell>
<Cell> <data type = "boolean1"> true </data> </cell>
</Row>
</Test>
Find all row nodes whose sun tzu node data meets type = string and inner text = Alpha.
XPath:
// Row [cell/data [text () = 'alpha'] and cell/data [@ type = 'string']
Or
// Row [cell/data [text () = 'alpha' and @ type = 'string']
Verification Program:
String xmlpayload = "<? XML version =/"1.0/" encoding =/"ISO-8859-1/"?> "+
"<Test>" +
@ "<Row>" +
"<Cell> <data type =/" String/"> alpha </data> </cell>" +
"<Cells> <data type =/" number/"> 100 </data> </cell>" +
"<Cells> <data type =/" number/"> 200 </data> </cell>" +
"<Cell> <data type =/" Boolean/"> </data> </cell>" +
"</Row>" +
"<Row>" +
"<Cell> <data type =/" String/"> gamma </data> </cell>" +
"<Cell> <data type =/" number/"> 12 </data> </cell>" +
"<Cell> <data type =/" number/"> 25 </data> </cell>" +
"<Cell> <data type =/" Boolean/"> 1 </data> </cell>" +
"</Row>" +
"</Test> ";
Xmldocument document = new xmldocument ();
Document. loadxml (xmlpayload );
String xmlpath = "// row [cell/data [text () = '1'] and cell/data [@ type = 'boolean']";
Xmlnodelist nodelist = Document. selectnodes (xmlpath );
Console. writeline ("nodelist. Count:" + nodelist. Count );
Console. Readline ();