【问题标题】:XamlReader.Load is throwing System.Windows.Markup.XamlParseExceptionXamlReader.Load 正在抛出 System.Windows.Markup.XamlParseException
【发布时间】:2015-04-02 06:47:44
【问题描述】:

以下可能有什么问题:

<Run FontWeight=\"Bold\" Foreground=\"#FF0000FF\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\"><Run.TextDecorations><TextDecoration Location=\"Underline\" /></Run.TextDecorations>046/98 5802007513 \r</Run>

虽然 XamlReader.Load 可以很好地加载类似的其他内容,但这会引发以下异常:

“类型的第一次机会异常 'System.Windows.Markup.XamlParseException' 发生在 PresentationFramework.dll

附加信息:给定编码中的无效字符。线 1,位置 233。”

复制问题的代码:

using System;
using System.IO;
using System.Text;
using System.Windows.Markup;

namespace XamlTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = "<Run FontWeight=\"Bold\" Foreground=\"#FF0000FF\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\"><Run.TextDecorations><TextDecoration Location=\"Underline\" /></Run.TextDecorations>046/98 5802007513 \r</Run>";
            Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(str));
            try
            {
                var temp = XamlReader.Load(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

【问题讨论】:

    标签: c# xaml load xamlreader


    【解决方案1】:

    调用 XamlReader.Parse 而不是 XamlReader.Load 不会抛出具有相同输入的异常“XamlParseException”,但是我不知道有什么区别以及它是如何工作的。

        static void Main(string[] args)
        {
            String str = "<Run FontWeight=\"Bold\" Foreground=\"#FF0000FF\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\"><Run.TextDecorations><TextDecoration Location=\"Underline\" /></Run.TextDecorations>046/98 5802007513 \r</Run>";
    
            try
            {
                var temp = XamlReader.Parse(str);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    

    【讨论】:

      【解决方案2】:

      使用 "(双引号)代替 \ 如下所示

      String str = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xml:space=""preserve"">046/98 5802007513 \r";

      【讨论】:

      • 我认为与双引号无关的是反斜杠!它没有用。
      • 尝试从“98 5802007513”中删除空间
      猜你喜欢
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多