Dealing with invalid XML hexadecimal characters

后端 未结 8 538
不知归路
不知归路 2020-12-05 11:10

I\'m trying to send an XML document over the wire but receiving the following exception:

\"MY LONG EMAIL STRING\" was specified for the \'Body\' element. ---         


        
8条回答
  •  执笔经年
    2020-12-05 11:42

    The following code removes XML invalid characters from a string and returns a new string without them:

    public static string CleanInvalidXmlChars(string text) 
    { 
         // From xml spec valid chars: 
         // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]     
         // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. 
         string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]"; 
         return Regex.Replace(text, re, ""); 
    }
    

提交回复
热议问题