164

From the following trials

<tag attr="\"">
<tag attr="<![CDATA["]]>">
<tag attr='"'>

Only the last one works for an XML parser that I'm using here. Is there an alternative?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jader Dias
  • 84,588
  • 150
  • 415
  • 615
  • 1
    **`"`** is the answer; [**here's the explanation**](https://stackoverflow.com/a/47534887/290085). – kjhughes May 13 '18 at 18:31
  • 1
    Possible duplicate: *[What characters do I need to escape in XML documents?](https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents)* – Peter Mortensen May 20 '20 at 17:17

4 Answers4

262

You can use &quot;

Sachin Shanbhag
  • 52,879
  • 11
  • 86
  • 103
55

From the XML specification:

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as "&apos;", and the double-quote character (") as "&quot;".

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Wim Coenen
  • 64,994
  • 13
  • 151
  • 241
28

A double quote character (") can be escaped as &quot;, but here's the rest of the story...

Double quote character must be escaped in this context:

  • In XML attributes delimited by double quotes:

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

Double quote character need not be escaped in most contexts:

  • In XML textual content:

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

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

    Similarly, (') require no escaping if (") are used for the attribute value delimiters:

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

See also

kjhughes
  • 98,039
  • 18
  • 159
  • 218
8

The String conversion page on the Coder's Toolbox site is handy for encoding more than a small amount of HTML or XML code for inclusion as a value in an XML element.

Kenny Evitt
  • 8,744
  • 5
  • 64
  • 89