Wednesday, May 13, 2015

How to use special characters in XML?

Problem Statement:

As XML uses some characters for tags and attributes into its syntax. So, it’s not possible to directly use those characters inside XML tags or attribute values.

Solution:

To use the special characters (such as Comma and Hyphen etc) inside XML tags one should must use the numeric reference instead of that character. The numeric character reference must be of UTF-8 because the supported encoding for XML files is defined in the prolog as encoding="UTF-8" and should not be changed.

Format:

The numeric character reference uses the format:
&#nn; for decimal form
&#xhh; for hexadecimal form


Code
Name
Result as
	
Horizontal tab
non-printing


Line feed
non-printing

Carriage Return
non-printing
 
Space
non-printing
!
Exclamation mark
!
"
Quotation mark
"
#
Number sign
#
$
Dollar sign
$
%
Percent sign
%
&
Ampersand
&
'
Apostrophe
'
(
Left parenthesis
(
)
Right parenthesis
)
*
Asterisk
*
+
Plus sign
+
,
Comma
,
-
Hyphen
-
.
Period
.
/
Slash
/
:
Colon
:
&#59;
Semi-colon
;
<
Less than
=
Equals sign
=
>
Greater than
?
Question mark
?
@
At
@
[
Left square bracket
[
\
Bbackslash
\
]
Right square bracket
]
^
Caret
^
_
Underscore
_
`
Acute accent
`
{
Left curly brace
{
|
Vertical bar
|
}
Right curly brace
}
~
Tilde
~