SaberTooth
SaberTooth

Reputation: 167

Read/Parse a resx file to identify

I'm trying to read or parse an XML resource (.resx) file (something similar to the snippet below), and identify how many of the nodes have the node in them. My options are either using python or a .Net console app.

  <data name="Carrier1" xml:space="preserve">
    <value>Carrier1Val</value>
    <comment>Parameter to display Carrier 1</comment>
  </data>
  <data name="Carrier2" xml:space="preserve">
    <value>Carrier2Val</value> 
    <comment>Parameter to display Carrier 2</comment>
  </data>
  <data name="Employee1" xml:space="preserve">
    <value>Employee1Val</value>
  </data>

Upvotes: 0

Views: 227

Answers (1)

Eldar
Eldar

Reputation: 10790

You can use Linq2Xml like below :

using System.Linq;
using System.Xml.Linq;

var xml = @"
<root>
 <data name=""Carrier1"" xml:space=""preserve"">
    <value>Carrier1Val</value>
    <comment>Parameter to display Carrier 1</comment>
  </data>
  <data name=""Carrier2"" xml:space=""preserve"">
    <value>Carrier2Val</value> 
    <comment>Parameter to display Carrier 2</comment>
  </data>
  <data name=""Employee1"" xml:space=""preserve"">
    <value>Employee1Val</value>
  </data>
</root>";
var doc = XElement.Parse(xml);
var result = doc.Elements("data").Where(r => r.Element("comment") is not null).Count();
// result = 2

Fiddle

Upvotes: 1

Related Questions