0

您好,我有一个来自 whihc 的 xml 文件,我想要其中的一部分。所以,这是我的 xml 文件:

           <Item Text="Reports" Navigateurl ="Reports/Default.aspx">
  <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" />
        <Item Text="Customer Outages"  
             NavigateUrl="Reports/Customers/CustomerOutages.aspx" Select="true"/>
      </Group>
    </Item>

为了选择这部分,我使用了这个表达式:

        XmlDataSource XmlDataSource1 = new XmlDataSource();
        XmlDataSource1.DataFile = "~/Menu.xml";
        XmlDataSource1.XPath = "//Item[@Text ='Customers']/Group";
        XmlDataSource1.DataBind();

结果将是

     Group
       Customers
       CustomerProducts
       ..........

但我只想要那些选择属性为真的。但我一直无法弄清楚如何做到这一点。你能帮我么?

4

3 回答 3

2

不确定我是否记得正确 - 但我认为这是这样做的方法。

XmlDataSource1.XPath = "//Item[@Text ='Customers']/Group[/Item/@Select='true']";
于 2012-05-09T12:54:31.910 回答
0

由于 XPATH 是 XPATH,我已经在 php 中制定了示例,因为我的 mac atm 上没有方便的 ac# 编译器。

路径语句"//Item[@Text ='Customers'][@Select='true']"导致:

Reports/Customers/Default.aspx
Reports/Customers/CustomerList.aspx

示例代码:

<?php

$xml = <<<XML
<Item Text="Reports" Navigateurl ="Reports/Default.aspx">
    <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" />
                <Item Text="Customer Outages" NavigateUrl="Reports/Customers/CustomerOutages.aspx" Select="true"/>
            </Group>
        </Item>
    </Group>
</Item>
XML;

$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$list = $xpath->query("//Item[@Text ='Customers'][@Select='true']");
foreach($list as $item)
{
    printf("%s\n", $item->getAttribute('NavigateUrl'));
}
于 2012-05-09T13:08:38.173 回答
0

这是正确的方法:

XmlDataSource1.XPath = "//Item[@Text ='Customers']/Group/Item[@Select = 'true']";
于 2012-05-09T13:06:54.057 回答