XamlReader.Parse 抛出“给定编码中的无效字符”

发布于 2024-11-04 09:26:55 字数 1528 浏览 0 评论 0原文

我对以下代码有问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-11-11 09:26:55

我对德语元音变音字符也遇到了同样的问题。我认为 .NET Framework 中存在错误。尝试使用此函数而不是 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文