Search code examples
c#globalization

Parsing Resx file with C# crashes on relative paths


Out of the sheer frustration of having to copy resx data into word to get the word count i've started to write my own tool to do so.

Well that made me run into an issue. i have icons and such things in the Resources.resx file. and they have relative paths according to the project they are being used int.

Which they should have obviously.

Well when i try to parse the Resx file in another application to count the words from the Value column.

i am getting errors as they can't parse the relative path. they end up going to folders that do not exist in my wordcount application.

Does any of you have an idea how i can fool the app into looking in the right folder when parsing these values?

i'm not quite sure why it is parsing those values to begin with. it should just grab the string that's all i care about.

i'm using the ResXReader

ResXResourceReader reader = new ResXResourceReader(filename);
foreach(System.Collections.DictionaryEntry de in reader)
{
   if (((string)de.Key).EndsWith(".Text"))
   {
      System.Diagnostics.Debug.WriteLine(string.Format("{0}: {1}", de.Key, de.Value));
   }
}

I found this here: Word Count of .resx files

It errors out on the foreach. ..\common\app.ico for example.

anyone have an idea on how to do this?


Solution

  • Alright. So the solution was a little easier than expected.

    1. i was using an outdated Class. I should have been using XDocument instead of XmlDataDocument

    secondly LINQ is the bomb.

    all i had to do was this:

    try
    {
        XDocument xDoc = XDocument.Load(resxFile);
    
        var result = from item in xDoc.Descendants("data")
        select new
        {
            Name = item.Attribute("name").Value,
            Value = item.Element("value").Value
        };
    
        resxGrid.DataSource = result.ToArray();           
    }
    

    and you can even allow empty strings if you cast those attributes/elements to (String)

    Hope this helps someone!