how to parse xml with special character in sql server

后端 未结 3 1313
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-20 01:07

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

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 01:52

    < needs to be specified as < in the XML

    
    
    W < hite
    Blue
    Black
    Green
    Red
    
    
    Apple
    Pineapple
    Grapes
    Melon
    
    
    

    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:

    
       < > & ' "
      
    
    

    Try it in a query:

    declare @xml xml =
    '
    
       < > & '' "
      
    
    '
    
    select @xml.value('(root/item)[1]', 'varchar(20)') as NodeValue,
           @xml.value('(root/item/@att)[1]', 'varchar(20)') as AttValue
    

    Result:

    NodeValue            AttValue
    -------------------- --------------------
     < > & ' "            < > & ' "
    

提交回复
热议问题