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?
koleto115Asked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
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
 
TimCotteeHead of Software ServicesCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.