Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:40:59+00:00 2026-05-22T14:40:59+00:00

I am trying to construct a Control template from code behind. Things were working

  • 0

I am trying to construct a Control template from code behind. Things were working fine till recently I found that the code was throwing an exception because of escape characters in string. The error message is dynamically constructed by retrieving from resource file.

The exception is

A first chance exception of type ‘System.Windows.Markup.XamlParseException’ occurred in PresentationFramework.dll
Additional information: Name cannot begin with the ‘@’ character, hexadecimal value 0x40. Line 1, position 537.

 //In this case when exception is thrown, 
 //string errorMessage = "Name cannot contain any of the following characters  $ \" @ ; ^ |   "


    public static ControlTemplate GetErrorTemplate(string errorMessage)
    {
        string xamlString = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
                                  "xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" " +
                                  "xmlns:nicefx=\"clr-namespace:NiceFx.Interop.UIComponents;assembly=NiceFx\" " +
                                  "xmlns:wpfkit=\"http://schemas.microsoft.com/wpf/2008/toolkit\" >" +
                                  " <DockPanel  LastChildFill=\"True\">" +
                                  "<TextBlock Foreground=\"White\" Background=\"Red\" FontSize=\"12\" Padding=\"2\" FontFamily=\"Trebuchet MS\"  Margin=\"5,5,0,0\" TextWrapping=\"Wrap\" DockPanel.Dock=\"Bottom\" Text=\"" + errorMessage + "\"></TextBlock>" +
                                  "<AdornedElementPlaceholder />" +
                                  " </DockPanel>" +
                                  " </ControlTemplate>";

        //EXCEPTION OCCURS IN THIS LINE
        ControlTemplate ct = (ControlTemplate)XamlReader.Load(XmlReader.Create(
            new StringReader(xamlString)));

        return ct;
    }

How do I escape this string? I tried all possible ways but I am unable to do so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:41:00+00:00Added an answer on May 22, 2026 at 2:41 pm

    According to the comment in your code, errorMessage contains a ", which will be inserted (without escaping it) into the XAML you are constructing. This " will then act as the closing quote of the Text attribute. At this point, the next non-whitespace character the parser encounters will be @, which is not an allowed character for the name of a XAML attribute, so it stops and reports the error.

    That covers the why. As for how to escape it, you can use the XML entity for double quote: &quot; Note that you may need to apply this escaping to multiple characters in your parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to construct a TreeView from a Menu. My Code is like
I am trying to construct a basic control that will display an undecorated JFrame
I'm trying to construct a query that will include a column indicating whether or
I am trying to construct a Groovy statement to find values that don't exist
I'm currently trying to construct a list of bean classes in Java from a
I'm trying to construct a database driven VB.Net app that pulls a list of
I have a custom WPF user control called a TimeoutPanel that I am trying
I'm working in VB.Net and I'm trying to make a piece of code more
I'm trying to control mem leaks in my code. In my general header file
I'm trying to build a compound control in Android, containing (among other things) a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.