I am getting following error when i add <
in my xml,
Msg 9455, Level 16, State 1, Line 6 XML parsing: line 4, character 14, illegal qual
You need to ensure the XML is valid, so you need to make sure any special characters are encoded.
e.g.
DECLARE @MyXML XML
SET @MyXML = '<SampleXML>
<Colors>
<Color1>W < hite</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Fruits>
<Fruits1>Apple</Fruits1>
<Fruits2>Pineapple</Fruits2>
<Fruits3>Grapes</Fruits3>
<Fruits4>Melon</Fruits4>
</Fruits>
</SampleXML>'
<
needs to be specified as <
in the XML
<SampleXML>
<Colors>
<Color1>W < hite</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Fruits>
<Fruits1>Apple</Fruits1>
<Fruits2>Pineapple</Fruits2>
<Fruits3>Grapes</Fruits3>
<Fruits4>Melon</Fruits4>
</Fruits>
</SampleXML>
Update:
The characters you need to escape in node values are <
=> <
and &
=> &
.
In attribute values you also need to escape "
=> "
if you use "
around your attribute values.
This is a valid XML:
<root>
<item> < > & ' "</item>
<item att=" < > & ' "" />
</root>
Try it in a query:
declare @xml xml =
'
<root>
<item> < > & '' "</item>
<item att=" < > & '' "" />
</root>
'
select @xml.value('(root/item)[1]', 'varchar(20)') as NodeValue,
@xml.value('(root/item/@att)[1]', 'varchar(20)') as AttValue
Result:
NodeValue AttValue
-------------------- --------------------
< > & ' " < > & ' "
Invalid special characters & its substitute in xml
&
<
>
"
'