Escape double quote character in XML

XmlEscaping

Xml Problem Overview


Is there an escape character for a double quote in xml? I want to write a tag like:

<parameter name="Quote = " ">

but if I put ", then that means string has ended. I need something like this (c++):

printf("Quote = \" ");

Is there a character to write before the double quote to escape it?

Xml Solutions


Solution 1 - Xml

Try this:

&quot;

Solution 2 - Xml

Here are the common characters which need to be escaped in XML, starting with double quotes:

  1. double quotes (") are escaped to &quot;
  2. ampersand (&) is escaped to &amp;
  3. single quotes (') are escaped to &apos;
  4. less than (<) is escaped to &lt;
  5. greater than (>) is escaped to &gt;

Solution 3 - Xml

Others have answered in terms of how to handle the specific escaping in this case.

A broader answer is not to try to do it yourself. Use an XML API - there are plenty available for just about every modern programming platform in existence.

XML APIs will handle things like this for you automatically, making it a lot harder to go wrong. Unless you're writing an XML API yourself, you should rarely need to worry about the details like this.

Solution 4 - Xml

New, improved answer to an old, frequently asked question...

When to escape double quote in XML

Double quote (") may appear without escaping:

  • In XML textual content:

    <NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded>
    
  • In XML attributes delimited by single quotes ('):

    <NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
    

    Note: switching to single quotes (') also requires no escaping:

    <NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
    

Double quote (") must be escaped:

  • In XML attributes delimited by double quotes:

    <EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>
    

Bottom line

Double quote (") must be escaped as &quot; in XML only in very limited contexts.

Solution 5 - Xml

No there isn't an escape character as such, instead you can use &quot; or even <![CDATA["]]> to represent the " character.

Solution 6 - Xml

If you just need to try something out quickly, here's a quick and dirty solution. Use single quotes for the attribute value:

<parameter name='Quote = " '>

Solution 7 - Xml

In C++ you can use EscapeXML ATL API. This is the correct way of handling special chars ...

Solution 8 - Xml

You can try using the a backslash followed by a "u" and then the unicode value for the character, for example the unicode value of the double quote is

" -> U+0022

Therefore if you were setting it as part of text in XML in android it would look something like this,

<TextView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text=" \u0022 Showing double quotes \u0022 "/>

This would produce a text in the TextView roughly something like this

" Showing double quotes "

You can find unicode of most symbols and characters here www.unicode-table.com/en

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
QuestionufukgunView Question on Stackoverflow
Solution 1 - XmlAndrew HareView Answer on Stackoverflow
Solution 2 - XmlAlex MuriithiView Answer on Stackoverflow
Solution 3 - XmlJon SkeetView Answer on Stackoverflow
Solution 4 - XmlkjhughesView Answer on Stackoverflow
Solution 5 - XmlMatt HowellsView Answer on Stackoverflow
Solution 6 - XmlBrad CupitView Answer on Stackoverflow
Solution 7 - XmlNevenView Answer on Stackoverflow
Solution 8 - XmlEAMView Answer on Stackoverflow