zoukankan      html  css  js  c++  java
  • XML非法字符的处理

    在解释XML时,会因为一些非法字符解析异常,因此在解析XML前处理非法字符十分重要。 

    XML的非法字符包括:

    1.需去除去的非法字符范围,在W3C手册XML的非法字符可以查找到:

          \x00-\x08

          \x0b-\x0c

          \x0e-\x1f

    2.需要替换的字符:

        字符    HTML字符 字符编码
        和 &     &   &
     单引号  ’     '     '
     双引号  ”      "     "
     大于号  >      >   >
     小于号  <      &lt;   &#60

    下面程序写个Demo(C#):

     static void Main(string[] args)
            {
                string str = "Canon "LCIXUS3< Soft>herx00 Carx08r'y Casex0b Su&its IXx1e10x0cIS>";
    
                string result = Regex.Replace(str, @"[x00-x08x0Bx0Cx0E-x1F]", "");
                result = result.Replace("&", "&amp;");
                result = result.Replace("'", "&apos;");
                result = result.Replace(""", "&quot;");
                result = result.Replace(">", "&gt;");
                result = result.Replace("<", "&lt;");
                Console.WriteLine(result);
                Console.ReadKey();
            }

        

  • 相关阅读:
    MySQL主从配置实现(同一台主机)
    MySQL主从配置实现
    FTP的安装配置使用
    NFS的安装配置使用
    Samba的安装配置使用
    Cacti的基本安装配置
    STL标准库-迭代器
    STL标准库-容器-unordered_set
    STL标准库-hash
    STL标准库-容器-rb_tree
  • 原文地址:https://www.cnblogs.com/wangqilong/p/10088342.html
Copyright © 2011-2022 走看看