DROP FUNCTION IF EXISTS dbo.fnRemoveXmlEvilCharacters
GO

CREATE FUNCTION dbo.fnRemoveXmlEvilCharacters (@text NVARCHAR(MAX))
  RETURNS NVARCHAR(MAX) AS
BEGIN

  DECLARE @return_value NVARCHAR(MAX)

  SET @return_value = 
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(
       @text
      ,CHAR(0x0), '')
      ,CHAR(0x1), '')
      ,CHAR(0x2), '')
      ,CHAR(0x3), '')
      ,CHAR(0x4), '')
      ,CHAR(0x5), '')
      ,CHAR(0x6), '')
      ,CHAR(0x7), '')
      ,CHAR(0x8), '')
      ,CHAR(0x9), '')
      ,CHAR(0xa), '')
      ,CHAR(0xb), '')
      ,CHAR(0xc), '')
      ,CHAR(0xd), '')
      ,CHAR(0xe), '')
      ,CHAR(0xf), '')
      ,CHAR(0x10), '')
      ,CHAR(0x11), '')
      ,CHAR(0x12), '')
      ,CHAR(0x13), '')
      ,CHAR(0x14), '')
      ,CHAR(0x15), '')
      ,CHAR(0x16), '')
      ,CHAR(0x17), '')
      ,CHAR(0x18), '')
      ,CHAR(0x19), '')
      ,CHAR(0x1a), '')
      ,CHAR(0x1b), '')
      ,CHAR(0x1c), '')
      ,CHAR(0x1d), '')
      ,CHAR(0x1e), '')
      ,CHAR(0x1f), '')
      ,CHAR(0x7f), '')
 
  RETURN @return_value

END