C # filters invalid characters for XML (hexadecimal value 0x1d)

Source: Internet
Author: User

The exception that is thrown when the XML is loaded or saved. System.ArgumentException: "(hexadecimal value 0x1d) is an invalid character.
The reason for this is that the XML file contains low nonprinting characters
Processing method: When the XML file is generated, filter low nonprinting characters

Replaces the Low-order ASCII character in a string with the & #x character
Convert ASCII 0-8->-
Convert ASCII 11-12->-
Convert ASCII 14-31->- [HTML] view plain copy return System.Text.RegularExpressions.Regex.Replace (httputilit Y.htmlencode (str), @ "[\x00-\x08]| [\x0b-\x0c]| [\x0e-\x1f] "," "");


[CSharp]   View plain copy/// <summary>  ///  replaces the   low-order bit  ASCII  character   in a string     character   ///  conversion   ASCII  0 - 8  ->   -   ///  Conversion   ASCII 11 - 12 ->   -    ///  Conversion   ASCII 14 - 31 ->  -   ///  </summary>  /// <param name= "tmp" ></param>  ///  <returns></returns>   Public static string replaceloworderasciicharacters ( string tmp)    {       StringBuilder info = new  StringBuilder ();       foreach  (char cc in tmp)        {           int ss =  ( INT) cc;           if  ((ss >= 0)  & &  (ss <= 8))  | |   ((ss >= 11)  &&  (ss <= 12))  | |   ((ss >= 14)  &&  (ss <= 32))                 info. AppendFormat ("{0:x};",  ss);           else info. Append (cc);       }       return info. ToString ();  }  /// <summary>  ///  replaces the following characters in a string with the   Low-order bit  ASCII  character   ///  conversion     -   -> ascii  0  - 8  ///  conversion     -    -> ascii 11 -  12  ///  Conversion &NBSP;&NBSP;&NBSP;-&NBSP;&NBsp;-> ascii 14 - 31  /// </summary>  /// <param  name= "Input" ></param>  /// <returns></returns>   public  static string getloworderasciicharacters (string input)    {        if  (String. IsNullOrEmpty (input))  return string. empty;       int pos, startIndex = 0, len =  Input. length;       if  (len <= 4)  return input;       stringbuilder result = new stringbuilder ();        while  (Pos = input. IndexOf ("",  startindex)  >= 0)        {            bool needReplace = false;    &Nbsp;      string roldv = string. Empty, rnewv = string. empty;           int le =  (Len - pos &NBSP;&LT;&NBSP;6)  ? len - pos : 6;            int p = input. IndexOf (";",  pos, le);  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.