0

以下内容可能有什么问题:

<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 可以很好地加载类似的其他内容,但这会引发以下异常:

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

附加信息:给定编码中的无效字符。第 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);
            }
        }
    }
}
4

2 回答 2

0

调用 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);
        }
    }
于 2016-02-24T08:09:33.450 回答
-1

使用 " (双引号)而不是 \ 如下所示

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

于 2015-04-02T07:08:57.837 回答