Search code examples
c#resx

When using the ResXResourceReader how can tell if the resource is an embedded file or if it is an embedded string


I have a separate application (that is for the purpose of spell checking my .resx files) that runs as a pre-build event. However, if the .resx file contains a text file (xml for example) my application will load the file and attempt to spell check it. This is not really what I want it to do. Is there a way to tell from the ResXResourceReader if the resource loaded is actually a file?

Code sample looks like this:

                ResXResourceReader reader = new ResXResourceReader(filename);
                ResourceSet resourceset = new ResourceSet(reader);

                Dictionary<DictionaryEntry, object> newvalues = new Dictionary<DictionaryEntry, object>();

                foreach (DictionaryEntry entry in resourceset)
                {
                    //Figure out in this 'if' if it is an embedded file and should be ignored.
                    if (entry.Key.ToString().StartsWith(">>") || !(entry.Value is string) || string.Compare((string)entry.Value, "---") == 0)
                        continue;
                 }

Solution

  • Yes. Setting UseResXDataNodes on ResXResourceReader will cause the dictionary values to be a ResXDataNode instead of the actual value, which you can use to determine if it is a file or not. Something like this:

    var rsxr = new ResXResourceReader("Test.resx");
    rsxr.UseResXDataNodes = true;
    foreach (DictionaryEntry de in rsxr)
    {
        var node = (ResXDataNode)de.Value;
        //FileRef is null if it is not a file reference.
        if (node.FileRef == null)
        {
            //Spell check your value.
            var value = node.GetValue((ITypeResolutionService) null);
        }
    }