XPATH EXPRESSION:删除其间的节点

时间:2012-05-09 13:55:43

标签: c# xpath

我有这个xml文件:

  <Item Text="Reports" Navigateurl ="Reports/Default.aspx" Select="true">
  <Group>
    <Item Text="Customers" NavigateUrl="Reports/Customers/Default.aspx" Select="true">
      <Group>
        <Item Text="Customers" NavigateUrl="Reports/Customers/CustomerList.aspx" 
            Select="true"   />
        <Item Text="Customer Products" 
         NavigateUrl="Reports/Customers/CustomersProducts.aspx" Select="true" />
        <Item Text="Customer Measurements" 
        NavigateUrl="Reports/Customers/CustomerLocationMeasurements.aspx" Select="true"  />
        <Item Text="Customer Groups" NavigateUrl="Reports/Customers/CustomerGroups.aspx" 
         Select="true"/>
        <Item Text="Customer Outages" NavigateUrl="Reports/Customers/CustomerOutages.aspx" 
         Select="true" />
      </Group>
    </Item>
    <Item Text="Rules" NavigateUrl="Reports/Rules/Default.aspx" Select="true" >
      <Group>
        <Item Text="Customer Product Rules" 
         NavigateUrl="Reports/Rules/CustomerProducts.aspx" Select="true"/>
        <Item Text="Customer Product Rules Event Limits" 
          NavigateUrl="Reports/Rules/CustomerProductEventLimits.aspx" Select="true" />
        <Item Text="Customer Product  Rules Curtailable Days/Hours" 
         NavigateUrl="Reports/Rules/CustomerProductCurtailableDaysHours.aspx" 
          Select="true"  />
      </Group>
    </Item>
     </Group>
  </Item>

所以,当我使用这个XPATH表达式时,

          XmlDataSource XmlDataSource1 = new XmlDataSource();
        XmlDataSource1.DataFile = "~/Menu.xml";
        XmlDataSource1.XPath = "//Item[@Text ='Reports']/Group/Item                  
                                  [@Select='true']       
        XmlDataSource1.DataBind();
        SiteMap1.DataSource = XmlDataSource1;

我得到了这个输出:

         Customers
            Group
               Customer Pages
          Rules
             Group
               Rules Pages

但我不希望Group标签显示在输出中。你可以告诉我这是如何实现的吗?

1 个答案:

答案 0 :(得分:1)

XPath是一种查询语言,用于选择节点或从XML文档中提取其他数据。因此,它无法修改文档的结构 - 例如删除节点

这种处理 - 称为转换,可以使用XSLT轻松完成,并且(不那么容易)使用XQuery。