How to display a text in XAML which contains double and single quotation marks?

WpfXaml

Wpf Problem Overview


In my XAML file I want to display this text which contains double and single quotation marks:

> You shouldn't choose "Copy if New".

None of these work:

<TextBlock Text="You shouldn't choose "Copy if New":"/>
<TextBlock Text="You shouldn't choose ""Copy if New"":"/>
<TextBlock Text="You shouldn't choose \"Copy if New\":"/>
<TextBlock Text='You shouldn't choose \"Copy if New\":'/>
<TextBlock Text='You shouldn\'t choose \"Copy if New\":'/>

I give up, can I do this in XAML?

Wpf Solutions


Solution 1 - Wpf

You should encode the special characters:

<TextBlock Text='You shouldn&apos;t choose &quot;Copy if New&quot;:'/>
  • &apos; for '
  • &quot; for "

Solution 2 - Wpf

There are defined XML escapes &amp; &quot; for " and &amp; &apos; for ' -- if the XML handling in XAML doesn't interpret those properly, then start to worry.

Solution 3 - Wpf

If you don't want to encode special chars you could use:

<TextBlock>
  <TextBlock.Text>
     You shouldn't choose "Copy if New":
  </TextBlock.Text>
</TextBlock>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - WpfJulien PoulinView Answer on Stackoverflow
Solution 2 - WpfSteve GilhamView Answer on Stackoverflow
Solution 3 - WpfIván GimenoView Answer on Stackoverflow