【问题标题】:select all xml nodes which contain a certain attribute选择所有包含某个属性的 xml 节点
【发布时间】:2013-04-11 09:52:57
【问题描述】:

我必须选择包含具有特定名称的属性的所有节点。

这是我目前的,无效的方法。

public List<string> RetrieveValuesForAttribute(string attributeName)
{
    var list = new List<string>();

    string xpath = "//*[@Name='" + attributeName + "']";
    XmlNodeList xmlNodeList = document.SelectNodes(xpath);

    foreach (XmlNode xmlNode in xmlNodeList)
    {
        list.Add(xmlNode.Attributes[attributeName].InnerText);
    }

    return list;
} 

我尝试选择包含方法参数attributeName 中给定名称的属性的所有节点,并将值添加到变量list

例子:

这个方法调用:

List<string> result = RetrieveValuesForAttribute("itemSelectedHandler");

应该返回一个包含字符串“OnSelectedRelatedContactChanged”的列表

这是xml文件:

<GroupBoxWrapper id="gbRelatedContacts" text="Related Contacts">
  <TabIndex>0</TabIndex>
  <TabStop>false</TabStop>
  <PanelWrapper id="pnlRelatedContactsView" width="1350">
    <TabIndex>0</TabIndex>
    <TabStop>false</TabStop>
    <ListViewWrapper id="lvRelatedContacts" itemSelectedHandler="OnSelectedRelatedContactChanged" itemDoubleClickHandler="OnRelatedContactDoubleClick">
      <TabIndex>0</TabIndex>
      <TabStop>true</TabStop>
      <ListViewColumns>
        <Column title="Name" mapNode="Contact\Name" />
        <Column title="Lastname" mapNode="Contact\Lastname" />
      </ListViewColumns>
    </ListViewWrapper>
  </PanelWrapper>
</GroupBoxWrapper>

其他问题: 用 LINQ 解决这个问题会更好吗?

解决方案1:谢谢你,ywm

public List<string> RetrieveValuesForAttribute(string attributeName)
{
    var list = new List<string>();

    string xpath = @"//*[@" + attributeName + "]";
    XmlNodeList xmlNodeList = document.SelectNodes(xpath);

    foreach (XmlNode xmlNode in xmlNodeList)
    {
        list.Add(xmlNode.Attributes[attributeName].InnerText);
    }

    return list;
}

解决方案 2:谢谢你,Jon Skeet

public List<string> RetrieveValuesForAttribute(string attributeName)
{
    //document is an XDocument
    return document.Descendants()
                   .Attributes(attributeName)
                   .Select(x => x.Value)
                   .ToList();
}

LINQ to XML 解决方案在我看来要优雅得多。

【问题讨论】:

  • 我肯定会为此使用 LINQ to XML。你能把document 改成XDocument 吗?
  • 是的,我可以,我会用 LINQ to XML 试试

标签: c# xml xpath


【解决方案1】:

如果您可以为此使用 LINQ to XML,那将是微不足道的:

// Note that there's an implicit conversion from string to XName,
// but this would let you specify a namespaced version if you want.
public List<string> RetrieveValuesForAttribute(XName attributeName)
{
    // Assume document is an XDocument
    return document.Descendants()
                   .Attributes(attributeName)
                   .Select(x => x.Value)
                   .ToList();
} 

【讨论】:

    【解决方案2】:

    您要查找的 XPath 应该是

    "//*[@" + attributeName + "]"
    

    您原来的 XPath 所做的是寻找所有具有 Name 属性和值为 attributeName 的元素

    这将查找具有attributeName 属性的任何元素

    //*[@title]
    

    将返回列元素

    【讨论】:

    • 谢谢您的解决方案,它运行良好。但我更喜欢 LINQ to XML 方法。
    【解决方案3】:

    我不确定 C# 语法,但我认为 xpath vlaue 是错误的。 请尝试:“//*[@itemSelectedHandler]”。 c#中应该有什么

      string xpath = "//*[@" + attributeName + "]";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多