I have a problem with the following code:
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
var content = reader.ReadToEnd();
ParserContext context = new ParserContext()
{
BaseUri = new Uri(Configuration.SkinsFolder)
//,XmlLang = "utf-8" // I have tried with this parameter and without it
};
var result = XamlReader.Parse(content, context);
return result;
}
The corresponding xaml, where problem appears:
...
<TextBlock>русская надпись<开发者_高级运维;/TextBlock>
<TextBlock Text="קח מספר" />
...
During parsing this xaml i get the exception:
Invalid character in the given encoding. Line 76, position 167.
at System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException)
at System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode)
at System.Windows.Markup.XamlParser.ReadXaml(Boolean singleRecordMode)
at System.Windows.Markup.XamlParser._Parse()
at System.Windows.Markup.XamlParser.Parse()
Xaml file saved as utf-8
Anybody knows how i can load this xaml without such problems? Thanks in advance!
PS: OK, i have found the source of the problem.
The correct way to load xaml is to use the XamlReader.Load method instead of the XamlReader.Parse. In my case it seems as:
using (Stream stream = new FileStream(source, FileMode.Open))
{
ParserContext context = new ParserContext()
{
BaseUri = new Uri(Configuration.SkinsFolder)
};
var result = XamlReader.Load(stream, context);
return result;
}
Thanks to all!
I've had the same problem with German umlaut characters. I think there's a bug in the .NET Framework. Try to use this function instead of XamlReader.Parse(content, context):
public static object Parse(string xamlText, ParserContext parserContext)
{
return System.Windows.Markup.XamlReader.Load((Stream) new MemoryStream(Encoding.UTF8.GetBytes(xamlText)), parserContext);
}
精彩评论