简体   繁体   中英

How can I filter xml data with xpath?

If I have a xml file with something like this:

 <?xml version="1.0" encoding="utf-8" ?> 
<Employees> 
   <Employee Department="Sales"> 
      <Name>David</Name> 
      <Salary>20000</Salary> 
   </Employee> 
   <Employee Department="Finance"> 
      <Name>Simon</Name> 
      <Salary>18000</Salary> 
   </Employee> 
   <Employee Department="Accounts"> 
      <Name>Peter</Name> 
      <Salary>22000</Salary> 
   </Employee> 
</Employees>

And then the data is being displayed in a repeater like so:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
   <ItemTemplate>
      <strong> 
     <%# XPath("@Department")%><br /> 
  </strong> 
  - Name: <%#XPath("Name")%><br /> 
  - Salary: <%#XPath("Salary")%><br />  
   </ItemTemplate>
</asp:Repeater>

How could I display only the Employee in the Sales department? This is in a asp.net webapp.

Try this by using XPath on the data source.

<asp:XmlDataSource DataFile="data.xml" runat="server" ID="XmlDataSource1" XPath="/Employees/Employee[@Department='Sales']" />

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
   <ItemTemplate>
    <strong>Department: <%# XPath("@Department")%><br /></strong> 
    - Name: <%#XPath("Name")%><br /> 
    - Salary: <%#XPath("Salary")%><br />  
   </ItemTemplate>
</asp:Repeater>

Output

Department: Sales
- Name: David
- Salary: 20000

在后台使用过滤器作为数据源Repeater的数据,这似乎无法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM