Why does en-dash (–) trigger illegal XML character error (C#/SSMS)?

后端 未结 4 585
星月不相逢
星月不相逢 2021-01-05 04:19

This is not a question on how to overcome the \"XML parsing: ... illegal xml character\" error, but about why it is happening? I know tha

4条回答
  •  既然无缘
    2021-01-05 05:08

    Can you modify the XML encoding declaration? If so;

    declare @xml XML = N'
      
      
    ';
    
    select @xml
    
    (No column name)
    
    

    Speculative Edit

    Both of these fail with illegal xml character:

    set @xml = ''
    set @xml = ''
    

    because they pass a non-unicode varchar to the XML parser; the string contains Unicode so must be treated as such, i.e. as an nvarchar (utf-16) (otherwise the 3 bytes comprising the are misinterpreted as multiple characters and one or more is not in the acceptable range for XML)

    This does pass a nvarchar string to the parser, but fails with unable to switch the encoding:

    set @xml = N''
    

    This is because an nvarchar (utf-16) string is passed to the XML parser but the XML document states its utf-8 and the is not equivalent in the two encodings

    This works as everything is utf-16

    set @xml = N''
    

提交回复
热议问题