If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.

XML escape characters

There are only five:

"   "
'   '
<   &lt;
>   &gt;
&   &amp;

Escaping characters depends on where the special character is used.

The examples can be validated at the W3C Markup Validation Service.

Text

The safe way is to escape all five characters in text. However, the three characters ", ' and > needn't be escaped in text:

<?xml version="1.0"?>
<valid>"'></valid>

Attributes

The safe way is to escape all five characters in attributes. However, the > character needn't be escaped in attributes:

<?xml version="1.0"?>
<valid attribute=">"/>

The ' character needn't be escaped in attributes if the quotes are ":

<?xml version="1.0"?>
<valid attribute="'"/>

Likewise, the " needn't be escaped in attributes if the quotes are ':

<?xml version="1.0"?>
<valid attribute='"'/>

Comments

All five special characters must not be escaped in comments:

<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>

CDATA

All five special characters must not be escaped in CDATA sections:

<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>

Processing instructions

All five special characters must not be escaped in XML processing instructions:

<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>

XML vs. HTML

HTML has its own set of escape codes which cover a lot more characters.

Answer from Welbog on Stack Overflow
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-escape
Best XML Escape characters tool
Online XML Escape characters tool to escape ampersand,quote and all special characters.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ xml-escape-unescape
XML Escape and XML Unescape Online Tool
Escape Unescape File.. Load URL ... Ad blocking? It's okay. ... XML Escape is easy to use tool to escape plain XML to escaped xml which helps to show xml text in XML in &ltpre&gt tag.
People also ask

How do you escape data in XML?
To escape data in XML, you must replace any special characters with their corresponding escape sequence. For example, the &lt; character should be replaced with &lt;, and the &gt; character should be replaced with &gt;. This can be done manually or through programming language functions or XML editor tools.
๐ŸŒ
testmuai.com
testmuai.com โ€บ home โ€บ free tools โ€บ xml escape
XML Escape Free Online | Free online tool to convert plain XML ...
What is the difference between XML and HTML escape?
Although XML and HTML share the same set of special characters, they have slightly different escape sequences. While the single quote (') character in XML can only be escaped with ', it can be escaped with either ' or ' in HTML. In addition, a few additional escape sequences used in HTML but not in XML include those for non-breaking spaces.
๐ŸŒ
testmuai.com
testmuai.com โ€บ home โ€บ free tools โ€บ xml escape
XML Escape Free Online | Free online tool to convert plain XML ...
What is the XML format?
XML (Extensible Markup Language) is a markup language used to define and structure data in a human-readable and machine-readable format. It consists of elements, attributes, and text content enclosed in tags, which describe the structure and content of the data.
๐ŸŒ
testmuai.com
testmuai.com โ€บ home โ€บ free tools โ€บ xml escape
XML Escape Free Online | Free online tool to convert plain XML ...
๐ŸŒ
Eternitech
eternitech.com โ€บ online-tools โ€บ xml-escape
XML Escape Characters Online | Free Online XML Escape
April 10, 2023 - XML Escape is a free online developer tool to escape special characters in XML. This tool delivers quick and accurate results in just a single click. copy, paste and escape.
๐ŸŒ
Testmuai
testmuai.com โ€บ home โ€บ free tools โ€บ xml escape
XML Escape Free Online | Free online tool to convert plain XML content to escaped HTML.
This free online tool allows you to convert characters with a special meaning in XML markup to their corresponding escape sequence.
Top answer
1 of 10
1710

If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.

XML escape characters

There are only five:

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

Escaping characters depends on where the special character is used.

The examples can be validated at the W3C Markup Validation Service.

Text

The safe way is to escape all five characters in text. However, the three characters ", ' and > needn't be escaped in text:

<?xml version="1.0"?>
<valid>"'></valid>

Attributes

The safe way is to escape all five characters in attributes. However, the > character needn't be escaped in attributes:

<?xml version="1.0"?>
<valid attribute=">"/>

The ' character needn't be escaped in attributes if the quotes are ":

<?xml version="1.0"?>
<valid attribute="'"/>

Likewise, the " needn't be escaped in attributes if the quotes are ':

<?xml version="1.0"?>
<valid attribute='"'/>

Comments

All five special characters must not be escaped in comments:

<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>

CDATA

All five special characters must not be escaped in CDATA sections:

<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>

Processing instructions

All five special characters must not be escaped in XML processing instructions:

<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>

XML vs. HTML

HTML has its own set of escape codes which cover a lot more characters.

2 of 10
118

New, simplified answer to an old, commonly asked question...

Simplified XML Escaping (prioritized, 100% complete)

  1. Always (90% important to remember)

    • Escape < as &lt; unless < is starting a <tag/> or other markup.
    • Escape & as &amp; unless & is starting an &entity;.
  2. Attribute Values (9% important to remember)

    • attr=" 'Single quotes' are ok within double quotes."
    • attr=' "Double quotes" are ok within single quotes.'
    • Escape " as &quot; and ' as &apos; otherwise.
  3. Comments, CDATA, and Processing Instructions (0.9% important to remember)

    • <!-- Within comments --> nothing has to be escaped but no -- strings are allowed.
    • <![CDATA[ Within CDATA ]]> nothing has to be escaped, but no ]]> strings are allowed.
    • <?PITarget Within PIs ?> nothing has to be escaped, but no ?> strings are allowed.
  4. Esoterica (0.1% important to remember)

    • Escape control codes in XML 1.1 via Base64 or Numeric Character References.
    • Escape ]]> as ]]&gt; unless ]]> is ending a CDATA section.
      (This rule applies to character data in general โ€“ even outside a CDATA section.)
๐ŸŒ
Cloudzenia
cloudzenia.com โ€บ tools โ€บ xml-escape
XML Escape Tool - Escape Special Characters in XML Online Free
Escape special characters in XML effortlessly with online XML Escape Tool. Ensure your XML data is safe, readable, and compatible for processing - CloudZenia
๐ŸŒ
tools
tools.fromdev.com โ€บ xml-escape-unescape.html
XML Escape UnEscape Online Developer Tools - FROMDEV
This is a free online tool to escape and unescape XML markup. You can copy/paste your desired XML in below text field and hit Escape or UnEscape button. HTML escaping is used to represent special meaning characters in a XML code. e.g. less than symbol < has a special meaning in XML markup language.
Find elsewhere
๐ŸŒ
Wtools
wtools.io โ€บ xml-escape-unescape
Online XML Escape/Unescape - wtools.io
Escapes or unescapes a XML string removing traces of offending characters that could be wrongfully interpreted as markup. The XML Escape / XML Unescape it's online tool was created to help with escape special characters into XML entities and also unescape it.
๐ŸŒ
@url-decode.com
url-decode.com โ€บ tool โ€บ xml-escape-unescape
Free Online XML Escape/Unescape Tool - URL Decoder
Click on the "Escape" button. The tool replaces the reserved characters in XML with their corresponding escaped characters.
๐ŸŒ
Coders Tool
coderstool.com โ€บ xml-escape-unescape
XML Escape Unescape Tool for Markup Cleanup and
Select โ€œUnescape XMLโ€ The tool will unescape XML online, decoding entities back into the original characters. Copy the decoded text Use the human-readable result for debugging, logs, or editing.
๐ŸŒ
Testmu
testmu.ai โ€บ home โ€บ free tools โ€บ xml escape
XML Escape Free Online | Free online tool to convert plain XML content to escaped HTML.
This free online tool allows you to convert characters with a special meaning in XML markup to their corresponding escape sequence.
๐ŸŒ
Text-Utils
text-utils.com โ€บ xml escape
XML Escape - Online Escape Tools | Text-Utils.com
September 6, 2025 - Copy shareable link is limited to 5,000 characters as it uses a client-side feature. ... XML Escape online: This tool can be used to escape XML characters with its associated entities.
๐ŸŒ
Liquid Technologies
liquid-technologies.com โ€บ Reference โ€บ Glossary โ€บ XML_EscapingData.html
Escaping XML Data
The '&' character must always be escaped. The other replacements (even the closing brace '>') are optional, but its good practice to always escape them. Data within a CDATA block can not be escaped. When the XML document is parsed (Character references are not expanded), so any chars within a CDATA block are just seen as character data.
๐ŸŒ
Freeonlineformatter
freeonlineformatter.com โ€บ xml-escape
XML Escape and Unescape Tool - Free Online Formatter
XML Free Online tool to escape or unescape XML documents, files and strings.
๐ŸŒ
Freedataformatter
freedataformatter.com โ€บ string-utilities โ€บ xml-escape.html
Online XML Escape and Unescape - Code Formatting Tools
Online Free XML Escape and Unescap.Our online XML Escapes or unescapes tool unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
๐ŸŒ
Advanced Installer
advancedinstaller.com โ€บ user-guide โ€บ xml-escaped-chars.html
XML escaped characters
For example, if you add an existing XML file or create a new one in your project and insert a special XML character in one of its elements, let's say < character, when you will build the project, Advanced Installer will automatically escape this character, replacing it with &lt;. The result is that the XML document installed by the built package will contain the escaped character making possible the correct interpretation of the XML code.
๐ŸŒ
W3Schools
w3schools.io โ€บ xml-escape-characters
Learn Which characters to escape for XML components - w3schools
This tutorial covers the essentials of Why Escape is required for XML and examples for Escape characters for content text, attributes, CDATA, and comments