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

标签 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>

我在选择 categoriesDropDown 时绑定(bind)了我的 attributeDropdown。代码如下

 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";

 }

现在的问题是名为ACAlarm 的两个属性属于房地产 两个类别>属性(property)出租。如何在选择这些类别时绑定(bind)这些属性?

如果属性在多个类别中,我的 Xpath 表达式应该是什么?

最佳答案

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

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

根据您的代码:

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

关于c# - 混淆 我的 Xpath 表达式应该是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140521/

相关文章:

.net - 我应该更频繁地处理 EF 上下文吗?

c# - 如何在不使用类别的情况下在属性网格中添加组?

c# - 自定义 Log4Net 附加程序未正确创建表或存储条目

c# - 在 ASP.NET Core 中获取当前星期几

c# - CSS:第一个选择器

c# - Excel 后台进程未关闭

C# 删除数据库失败

asp.net - Css 事件 isse :not working

c# - 完成前取消 $.post

c# - 在 .NET 中将 Markdown 转换为 HTML