C# 选择所有包含某个属性的 xml 节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15945733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select all xml nodes which contain a certain attribute
提问by Joel
I have to select all nodes which contain an attribute with a certain name.
我必须选择包含具有特定名称的属性的所有节点。
This is my current, not working approach.
这是我目前的,不工作的方法。
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;
}
I try to select all nodes which contain the attribute with the name given in the method parameter attributeName
and add the value the variable list
.
我尝试选择包含具有方法参数中给定名称的属性的所有节点,attributeName
并将值添加到变量中list
。
Example:
例子:
This method call:
这个方法调用:
List<string> result = RetrieveValuesForAttribute("itemSelectedHandler");
Should return an list which contains the string "OnSelectedRelatedContactChanged"
应该返回一个包含字符串“OnSelectedRelatedContactChanged”的列表
This is the xml file:
这是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>
Further questions: Would it be better to solve this with LINQ?
进一步的问题:用 LINQ 解决这个问题会更好吗?
Solution 1:thank you, ywm
解决方案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;
}
Solution 2:thank you, Jon Skeet
解决方案 2:谢谢你,乔恩·斯基特
public List<string> RetrieveValuesForAttribute(string attributeName)
{
//document is an XDocument
return document.Descendants()
.Attributes(attributeName)
.Select(x => x.Value)
.ToList();
}
The LINQ to XML Solution looks far more elegant to me.
LINQ to XML 解决方案在我看来要优雅得多。
采纳答案by Jon Skeet
If you could use LINQ to XML for this, it would be utterly trivial:
如果您可以为此使用 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();
}
回答by ywm
The XPath you are looking for should be
您正在寻找的 XPath 应该是
"//*[@" + attributeName + "]"
What your original XPath was doing was looking for all elements that have a Name
attribute with the value attributeName
您原来的 XPath 正在做的是寻找具有Name
值的属性的所有元素attributeName
This will look for any element which has an attribute with attributeName
这将查找任何具有 attributeName 属性的元素
//*[@title]
would return the column elements
将返回列元素
回答by hr_117
Im not sure about the C# syntax but I think the xpath vlaue is wrong. Please try: "//*[@itemSelectedHandler]". What should in c#
我不确定 C# 语法,但我认为 xpath vlaue 是错误的。请尝试:“///*[@itemSelectedHandler]”。在 C# 中应该做什么
string xpath = "//*[@" + attributeName + "]";