XamlReader.Parse 抛出“给定编码中的无效字符”
我对以下代码有问题:
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;
}
相应的xaml,其中出现问题:
...
<TextBlock>русская надпись</TextBlock>
<TextBlock Text="קח מספר" />
...
在解析此xaml期间,我得到异常:
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文件保存为utf-8
有人知道我如何加载此xaml而不会出现此类问题吗? 提前致谢!
PS:好的,我已经找到问题的根源了。
加载xaml的正确方法是使用XamlReader.Load方法而不是XamlReader.Parse。就我而言,这似乎是:
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;
}
感谢所有人!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对德语元音变音字符也遇到了同样的问题。我认为 .NET Framework 中存在错误。尝试使用此函数而不是 XamlReader.Parse(content, context):
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):