Does .NET SelectNodes() allow you to select attributes?
Does it pull al开发者_StackOverflow社区l node types supported by Xpath? Seems that when I call
selectNodes("@")
it just returns the elements that contain the attributes, but I want to get the list of attributes themselves. In other words I would expect the resulting node collection to contain nodes of type Attr only...but that does not seem to work.
foreach(XmlAttribute att in selectNodes("*/@*")) ...
From the Documentation:
Selects a list of nodes matching the XPath expression.
While the Attributes property (of a Node)
Gets an XmlAttributeCollection containing the attributes of this node.
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.attributes.aspx
To get all attributes in a document (I think that's what you're trying to do?) you may want to try
selectNodes("@*")
The correct XPath syntax for selecting context node's attributes is:
@*
Or
attribute::*
But do note that only elements might have attributes. So, if the context node is the document root, you won't select anything, of course.
If you want all the attributes from a document, use:
//@*
That will be expanded to:
/descendant-or-self::node()/attribute::*
精彩评论