GeorgDangl
GeorgDangl

Reputation: 2192

Validating Xml Documents via System.Xml.XmlReader and Generating Error Messages in German

We've got a project in which we do some XML schema validation. We're getting an Xml file as Stream at runtime, load it, find the corresponding XSD schema document and then process to parse it and log any validation errors we encountered like this:

var namespaceName = xDocument.Root.GetDefaultNamespace().NamespaceName;

var xmlReaderSettings = XmlReaderSettingsResolver.GetXmlReaderSettings(namespaceName, fileInformations);

xmlReaderSettings.ValidationEventHandler += (sender, args) =>
{
    validationResult.Errors.Add(new CustomXmlSchemaError
    {
        // 'args.Message' is always in English, no matter which culture is set
        Message = args.Message,
        LineNumber = args.Exception.LineNumber,
        LinePosition = args.Exception.LinePosition
    });
};

using var xmlStreamCopy = new MemoryStream();
using var streamWriter = new StreamWriter(xmlStreamCopy, Encoding.UTF8);
streamWriter.Write(fileAsString);
streamWriter.Flush();
xmlStreamCopy.Position = 0;
var xmlReader = XmlReader.Create(xmlStreamCopy, xmlReaderSettings);

// Read the whole file, the validation is done in the event handler while the file is being processed
while (xmlReader.Read()) { }

I would have expected to be able to get the error messages in German languages by setting the culture to German:

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de");

However, that doesn't seem to help. I've seen other .NET applications produce similar features in e.g. German, so I guess there's some way to use the correct resources, but I've been unable to find a way.

We're running this both on .NET Framework as well as on .NET / .NET Core, in Windows and Linux operating systems.

Upvotes: 0

Views: 86

Answers (0)

Related Questions