2015-04-02 40 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

相關問題