Trouble extracting the correct XmlNode using XPath and C#

I am trying to use XPath and C# to extract the second label with the name "searchbox-search-results-button"

<?xml version="1.0" encoding="utf-8"?>
<content-item>
  <createDate>
    11/23/2011
  </createDate>
  <lastModified>
    12/12/2011
  </lastModified>
  <displayTitle>
    Options
  </displayTitle>
  <author>
    me
  </author>

  <labels>
    <label name="TEST-LABEL-FOR-UNIT-TEST">
      <![CDATA[TEST LABEL FOR UNIT TEST
]]>
    </label>
    <label name="searchbox-search-results-button">
      <![CDATA[Search Results
]]>
    </label>
  </labels>
</content-item>


I am using the following but only getting all the labels, not the specifc one I want to get.

var searchStr = //content-item/labels[label/@name='searchbox-search-results-button']
var xmlDoc = new XmlDocument();
xmlDoc.Load(DataPath + xmlFile);
return xmlDoc.SelectSingleNode(searchStr);

How do I use XPath to find a single node???
newbiewebSr. Software EngineerAsked:
Who is Participating?
 
sybeCommented:
sorry:

/content-item/labels/label[@name='searchbox-search-results-button']
0
 
sybeCommented:
//content-item/labels[label/@name='searchbox-search-results-button']

should be

/content-item/labels[label/@name='searchbox-search-results-button']


0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks. But the InnerText property has the text for all lable, not the single one associated with 'searchbox-search-results-button'. I don't get it.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
 
newbiewebSr. Software EngineerAuthor Commented:
That is still not right. I re-arranged the sequence of the XML:

<?xml version="1.0" encoding="utf-8"?>
<content-item>
  <createDate>
    11/23/2011
  </createDate>
  <lastModified>
    12/12/2011
  </lastModified>
  <displayTitle>
    Options
  </displayTitle>
  <author>
    me
  </author>

  <labels>
    <label name="searchbox-search-results-button">
      <![CDATA[Search Results
]]>
      <label name="TEST-LABEL-FOR-UNIT-TEST">
      <![CDATA[TEST LABEL FOR UNIT TEST
]]>
    </label>
    </label>
  </labels>
</content-item>


And the InnerText contained the CDATA for both labels. I want just the one requested. Do you know how to fix this?
0
 
newbiewebSr. Software EngineerAuthor Commented:
Sorrry, i used bad XML. It seems to work fine. Thanks.
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.