Xanx
Xanx

Reputation: 565

.Net Core Resources give null instead of a string

I'm trying to use Resources (.resx) in my .Net Core console app, but for some reason I'm not able to get string from a resource, instead, I'm always getting null. Here is example code that reproduce the issue:

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Resources.Message);
        }
    }
}

And here is Resources.resx file content:

<?xml version="1.0" encoding="utf-8"?>

<root>
    <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:element name="root" msdata:IsDataSet="true">
            <data name="Message">
                <value>Hello world!</value>
            </data>
        </xsd:element>
    </xsd:schema>
    <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
    </resheader>
    <resheader name="version">
        <value>1.3</value>
    </resheader>
    <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>
    <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>
</root>

I'm using Mac and dotnet core 2.2.100 (seems to be the latest version). Adding Microsoft.Extensions.Localization didn't help.

Upvotes: 1

Views: 541

Answers (1)

Belurd
Belurd

Reputation: 782

Your string is defined in the wrong place in xml:

<root>   
  ...
  <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="Message" xml:space="preserve">
    <value>Hello World!</value>
  </data>
</root>

Upvotes: 1

Related Questions