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

Matthew Sanford picture Matthew Sanford · Nov 16, 2011 · Viewed 7.9k times · Source

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;
                 }

Answer

vcsjones picture vcsjones · Nov 16, 2011

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);
    }
}