tags:

views:

57

answers:

2

I'm trying to create a .resx file from a dictionary of strings. Luckily the .Net framework has a ResXResourceWriter class. The only problem is that when I create the file using the code below, the last few lines of my generated resx file are missing. I checked my Dictionary and it contains all the string pairs that I expect.

public void ExportToResx(Dictionary<string,string> resourceDictionary, string fileName)
{
    var writer = new ResXResourceWriter(fileName);

    foreach (var resource in resourceDictionary)
    {
        writer.AddResource(resource.Key, resource.Value);
    }
}

Unfortunately, it is a little difficult to show the entire resx file since it has 2198 (should have 2222) lines but here is the last little bit:

...
2195    <data name="LangAlign_ReportIssue" xml:space="preserve">
2196        <value>Report an Issue</value>
2197    </data>
2198    <data name="LangAlign_Return

BTW, notice that the file cuts off right at the end of that "n" in "LangAlign_Return". The string should read "LangAlign_ReturnToWorkspace". The file should also end at line 2222.

A: 

Does "LangAlign_ReturnToWorkspace" actually say that, or is it "LangAlign_Return ToWorkspace"?

(With a space or perhaps a non-printing character between Return and ToWorkspace?)

Maybe that is causing the elememt name to be generated wrongly?

Just an idea.

Andy
I just double checked this suggestion but there are no non-printing characters before or after in this string. When I read your suggestion I half expected it to be the problem.Thank you,Aaron
Aaron Salazar
Ah, too bad, worth a try. Does it present the same error if you only put this value in the Dictionary?
Andy
I edited the XML file that I used to create the Dictionary (ensuring any strange chars remain) down to the offending entry. I also kept all of the other entries that were below this one. The file was cut off in a different spot and in the middle of one of the XML attributes. So, I know that it isn't my strings because it cut off at a spot that was created by the ResXResourceWriter class. Hmmm, I wonder what is up with this. My code could not be more simple.
Aaron Salazar
A: 

Try this:

public void ExportToResx(Dictionary<string,string> resourceDictionary, string fileName)
{
    using(var writer = new ResXResourceWriter(fileName))
    {
        foreach (var resource in resourceDictionary)
        {
            writer.AddResource(resource.Key, resource.Value);
        }
    }
}

You needed to call Dispose() on the writer. :)

Jason Bunting