How to select a value of an attribute with XPath?

Posted on 2008-11-11
Last Modified: 2013-11-18
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?
Question by:koleto115
    LVL 44

    Expert Comment

    Hello koleto115,


    Should do it.



    Author Comment

    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 ...
    LVL 44

    Accepted Solution


    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.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System ( introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now