Question: I am working on a C# code. I have my XML document with with different nodes, Out of which I need to count the number of nodes having the same innertext value. I am Using XmlDocument. Let's say I have a xml file where I have to count the number of nodes having InnerText value BSS.

Code:

```
<Annotations>
 <Objects>
  <ObjectId>BSS</ObjectId>
  <ObjectId>CAT</ObjectId>
 </Objects>
<Objects>
  <ObjectId>BSS</ObjectId>
  <ObjectId>ABC</ObjectId>
 </Objects>
<Objects>
  <ObjectId>BSS</ObjectId>
  <ObjectId>PCT</ObjectId>
 </Objects>
</Annotations>

```

Here in the Above code I want to count the number of nodes having innertext value BSS.like here the count should be 3.Your help will be greatly appreciated.

Edited Code:

```
<Annotations>
 <Objects>
  <ObjectId>BSS|SAF|PAT</ObjectId>
  <ObjectId>CAT</ObjectId>
 </Objects>
<Objects>
  <ObjectId>BSS|SAF|PAT</ObjectId>
  <ObjectId>ABC</ObjectId>
 </Objects>
<Objects>
  <ObjectId>BSS|SAF|PAT</ObjectId>
  <ObjectId>PCT</ObjectId>
 </Objects>
</Annotations>

```

Answer: 
If you have it loaded as an `XmlDocument`, you can do

```
doc.selectNodes("//*[.='BSS']").Count;

```

Although I think there might be a more efficient way of writing that XPath, matching everything then checking the text content of it feels like it's probably inefficient.

If that's a complete and representative document, then you can do

```
doc.selectNodes("Annotations/Objects/ObjectId[.='BSS']").Count;

```

This will be more efficient, as it's targetting specific elements rather than the entire document.

If the search text is contained within a pipe (`|`) separated list, you can use this predicate instead of `[.='BSS']`:

```
[contains(concat('|',.,'|'),'|BSS|')]

```

Although this will be slower. Personally, I'd recommend that wherever your source comes from, it be modified to separate this list into separate elements, it'll make querying your data much easier. I'm a strong advocate of the principle that XML documents should follow the same principle as the 'First Normal Form' rule that databases do, and not include multiple values in any one text node/attribute.
I would use the new `Linq to XML` api instead :

```
var xDocument = XDocument.Load("path");

int count = xDocument.Descendants().Count(x => (string)x == "BSS");

```