Link to home
Start Free TrialLog in
Avatar of koleto115
koleto115

asked on

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?
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello koleto115,

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

Should do it.

Regards,

TimCottee
Avatar of koleto115
koleto115

ASKER

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 ...
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial