混淆我的Xpath表达式应该是什么?

时间:2012-02-04 11:30:51

标签: c# asp.net .net xml xpath

我的categories.xml文件在下面给出

<categories>
  <root name="Cars -Vehicles" id="CV"></root>
  <root name="Personals" id="PER"></root>
  <root name="Real Estate" id="RE"></root>
  <root name="Property For Rent" id="PFR"></root>
  <root name="Community" id="COM"></root>
</categories>

和我的attibutes.xml文件在下面给出

<attributes>

  <attribute value="ethnicity=ethnicity_african_american" name ="American Indian" categoryid="PER"/>
  <attribute value="ethnicity=ethnicity_asian_pacific_islander" name ="Asian - Pacific Islander" categoryid="PER"/>
  <attribute value="ethnicity=ethnicity_caucasian" name ="Caucasian" categoryid="PER"/>


  <attribute value="amenities=amenities_ac" name ="AC" categoryid="RE, PFR"/>
  <attribute value="amenities=amenities_alarm" name ="Alarm" categoryid="RE, PFR"/>

</attributes>

我将attributeDropdown绑定在categoriesDropDown的选择上。代码如下:

 protected void ddCategories_SelectedIndexChanged(object sender, EventArgs e)
 {
            XmlDataSource xd = new XmlDataSource();
            xd.DataFile = Server.MapPath("Xmls") + "\\attributes.xml";
            xd.XPath = "/attributes/attribute[@categoryid='" + 
                        categoriesDropDown.SelectedValue.ToString() + "']";

            attributeDropdown.DataSource = xd;
            attributeDropdown.DataTextField = "name";
            attributeDropdown.DataValueField = "value";

            attributeDropdown.DataBind();

         // categoriesDropDown.DataTextField = "name";
         // categoriesDropDown.DataValueField = "id";

 }

现在问题是名为 AC 的两个属性和警报分为两类 房地产 物业出租 。如何在选择这些类别时绑定这些属性?

如果属性属于多个类别,请说明我的Xpath Expression应该是什么?

1 个答案:

答案 0 :(得分:1)

也许这不是一个好的解决方案,但你可以使用contains函数,例如:

/attributes/attribute[contains(@categoryid, 'category')]

根据你的代码:

xd.XPath = string.Format("/attributes/attribute[contains(@categoryid, '{0}')]",
    categoriesDropDown.SelectedValue);