Search code examples
.netresx

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>

Solution

  • 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