当后代innertext等于value时选择parent

时间:2015-04-26 11:28:50

标签: c# xml xpath

我正在尝试生成一个xpath查询,当子属性innertext等于某个值时,可以执行该查询以选择父节点。 XML如下:

<order>
  <orderitem>
    <ordername>Georgina Sinclair</ordername>
    <ordernumber>408999703657</ordernumber>
    <orderpaymentmethod>cheque</orderpaymentmethod>
    <bankname>First National Bank</bankname>
    <branch>Fourways</branch>
    <amount>275.00</amount>
    <date>12/01/2012</date>
  </orderitem>
  <orderitem>
    <ordername>Zachary Whitehead</ordername>
    <ordernumber>409122372301</ordernumber>
    <orderpaymentmethod>cheque</orderpaymentmethod>
    <bankname>ABSA</bankname>
    <branch>Irene</branch>
    <amount>70.25</amount>
    <date>12/01/2012</date>
  </orderitem>
  <orderitem>
    <ordername>Toby Henderson</ordername>
    <ordernumber>401255489873</ordernumber>
    <orderpaymentmethod>cheque</orderpaymentmethod>
    <bankname>First National Bank</bankname>
    <branch>Edenvale</branch>
    <amount>181.03</amount>
    <date>12/13/2012</date>
  </orderitem>
  <orderitem>
    <ordername>Katherine Cooke</ordername>
    <ordernumber>409155874935</ordernumber>
    <orderpaymentmethod>savings</orderpaymentmethod>
    <bankname>ABSA</bankname>
    <branch>Southdowns</branch>
    <amount>975.89</amount>
    <date>01/01/2013</date>
  </orderitem>
  <orderitem>
    <ordername>Bradley James</ordername>
    <ordernumber>409254998</ordernumber>
    <orderpaymentmethod>savings</orderpaymentmethod>
    <bankname>ABSA</bankname>
    <branch>Melville</branch>
    <amount>207.74</amount>
    <date>12/09/2012</date>
  </orderitem>
  <orderitem>
    <ordername>Sophie Lane</ordername>
    <ordernumber>409771987</ordernumber>
    <orderpaymentmethod>savings</orderpaymentmethod>
    <bankname>ABSA</bankname>
    <branch>Roodepoort</branch>
    <amount>207.74</amount>
    <date>12/31/2012</date>
  </orderitem>
</order>

我想选择银行名称等于First National Bank的orderitem元素。是否有一个xpath方法在没有LINQ的情况下在c#中执行此操作?我正在努力获得这种类型的xpath查询

1 个答案:

答案 0 :(得分:2)

我不确定您是在寻找非LINQ C#方法,还是在寻找实际的XPath表达式,所以......

C#

取决于您是要使用XmlDocument还是XDocument

的XPath

/order/orderitem[bankname='First National Bank']