2015-04-02 28 views
-1

出了什么问题有以下:XamlReader.Load抛出System.Windows.Markup.XamlParseException

<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

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

回答

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); 
     } 
    } 
-1

使用 “(双引号),而不是\如下

字符串str = @显示” HTTP: //schemas.microsoft.com/winfx/2006/xaml/presentation“”xml:space =“”preserve“”> 046/98 5802007513 \ r“;

+0

我认为它与双引号无关是一个反斜杠!它没有工作。 – Abbas 2015-04-02 07:59:09

+0

尝试从“98 5802007513”中删除空格 – user3150546 2015-04-02 08:11:39

相关问题