Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

resx

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;
                 }
like image 221
Matthew Sanford Avatar asked Nov 16 '11 20:11

Matthew Sanford


People also ask

What is a .resx file and how do you use it?

The . resx resource file format consists of XML entries that specify objects and strings inside XML tags. One advantage of a . resx file is that when opened with a text editor (such as Notepad) it can be written to, parsed, and manipulated.

How do I view a RESX file?

Navigate to resources from File Structure Open a . resx file in the XML (Text) editor. Press Ctrl+Alt+F or choose ReSharper | Windows | File Structure from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there.

What are RESX files?

Net Resource (. resx) files are a monolingual file format used in Microsoft . Net Applications. The . resx resource file format consists of XML entries, which specify objects and strings inside XML tags.


1 Answers

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);
    }
}
like image 111
vcsjones Avatar answered Sep 28 '22 05:09

vcsjones