0

What could be wrong with the following:

<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>

While similar others are loaded fine by the XamlReader.Load, this throws following exception:

"A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Invalid character in the given encoding. Line 1, position 233."

Code to replicate the issue:

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);
            }
        }
    }
}
Abbas
  • 3,872
  • 6
  • 36
  • 63

2 Answers2

0

Calling XamlReader.Parse instead of XamlReader.Load doesn't throw exception "XamlParseException" with the same input, however I don't know what's the difference and how it is working.

    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);
        }
    }
Abbas
  • 3,872
  • 6
  • 36
  • 63
-1

use " (Double quote) instead of \ as show below

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