[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3991
  • Last Modified:

How to select a value of an attribute with XPath?

I have xml like this one:


<DetectorData CollectionID='1'/>

whit XSLT one can get the string value like this:

<xsl:value-of select="DetectorData/@CollectionID"/>

How to get the string value with XPath and c# with similar construction?

node.SelectSingleNode("DetectorData/@CollectionID") .... does this works?
0
koleto115
Asked:
koleto115
  • 2
1 Solution
 
TimCotteeCommented:
Hello koleto115,

node.SelectSingelNode("DetectorData").Attributes["CollectionID"].InnerText

Should do it.

Regards,

TimCottee
0
 
koleto115Author Commented:
Yes, but is there a simple construction that returns string.Empty?
I don't want to check whether the attribute is there or not...
The dotted notation is very ugly, I hopped that there is a kind of XPath trick to retrieve the attribute value with a single method ...
somthing like node.SelectSingleNode("DetectorData/@CollectionID" ..... something that like that ...
0
 
TimCotteeCommented:
koleto115,

Not as far as I am aware, there are methods to select specific nodes based on attribute values but I don't believe that you can use it to return the value of an attribute that way.

TimCottee
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now