原文:C#十六进制值0x12,是一个无效字符 - 程序园

我正在加载很多xml文档,其中一些返回错误,如“十六进制值0x12,是无效字符”,并且有不同的字符。如何删除它们?
 
我在这里做了一个小的研究。

这是ASCII表。有128个符号

这是一些小的测试代码,它会添加ASCII表中的每个符号,并尝试将其作为XML文档加载。

static public void RegexTry()
{
StreamReader stream = new StreamReader(@"test.xml");
string xmlfile = stream.ReadToEnd();
stream.Close(); string text = ""; for (int i = 0; i < 128; i++ )
{
char t = (char) i; text = xmlfile.Replace('П', t); XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(text);
}
catch (Exception ex)
{
Console.WriteLine("Char("+i.ToString() +"): " + t + " => error! " + ex.Message);
continue;
} Console.WriteLine("Char(" + i.ToString() + "): " + t + " => fine!");
} Console.ReadKey();
}

因此,它返回:

Char(0): => error! '.', hexadecimal value 0x00, is an invalid character. Line 5, position 7.
Char(1): => error! '', hexadecimal value 0x01, is an invalid character. Line 5, position 7.
Char(2): => error! '', hexadecimal value 0x02, is an invalid character. Line 5, position 7.
Char(3): => error! '', hexadecimal value 0x03, is an invalid character. Line 5, position 7.
Char(4): => error! '', hexadecimal value 0x04, is an invalid character. Line 5, position 7.
Char(5): => error! '', hexadecimal value 0x05, is an invalid character. Line 5, position 7.
Char(6): => error! '', hexadecimal value 0x06, is an invalid character. Line 5, position 7.
Char(7): => error! '', hexadecimal value 0x07, is an invalid character. Line 5, position 7.
Char(8): => error! '', hexadecimal value 0x08, is an invalid character. Line 5, position 7.
Char(9): => fine!
Char(10):
=> fine!
Char(11): => error! '', hexadecimal value 0x0B, is an invalid character. Line 5, position 7.
Char(12): => error! '', hexadecimal value 0x0C, is an invalid character. Line 5, position 7.
Char(13):
=> fine!
Char(14): => error! '', hexadecimal value 0x0E, is an invalid character. Line 5, position 7.
Char(15): => error! '', hexadecimal value 0x0F, is an invalid character. Line 5, position 7.
Char(16): => error! '', hexadecimal value 0x10, is an invalid character. Line 5, position 7.
Char(17): => error! '', hexadecimal value 0x11, is an invalid character. Line 5, position 7.
Char(18): => error! '', hexadecimal value 0x12, is an invalid character. Line 5, position 7.
Char(19): => error! '', hexadecimal value 0x13, is an invalid character. Line 5, position 7.
Char(20): => error! '', hexadecimal value 0x14, is an invalid character. Line 5, position 7.
Char(21): => error! '', hexadecimal value 0x15, is an invalid character. Line 5, position 7.
Char(22): => error! '', hexadecimal value 0x16, is an invalid character. Line 5, position 7.
Char(23): => error! '', hexadecimal value 0x17, is an invalid character. Line 5, position 7.
Char(24): => error! '', hexadecimal value 0x18, is an invalid character. Line 5, position 7.
Char(25): => error! '', hexadecimal value 0x19, is an invalid character. Line 5, position 7.
Char(26): => error! '', hexadecimal value 0x1A, is an invalid character. Line 5, position 7.
Char(27): => error! '', hexadecimal value 0x1B, is an invalid character. Line 5, position 7.
Char(28): => error! '', hexadecimal value 0x1C, is an invalid character. Line 5, position 7.
Char(29): => error! '', hexadecimal value 0x1D, is an invalid character. Line 5, position 7.
Char(30): => error! '', hexadecimal value 0x1E, is an invalid character. Line 5, position 7.
Char(31): => error! '', hexadecimal value 0x1F, is an invalid character. Line 5, position 7.
Char(32): => fine!
Char(33): ! => fine!
Char(34): " => fine!
Char(35): # => fine!
Char(36): $ => fine!
Char(37): % => fine!
Char(38): => error! An error occurred while parsing EntityName. Line 5, position 8.
Char(39): ' => fine!
Char(40): ( => fine!
Char(41): ) => fine!
Char(42): * => fine!
Char(43): + => fine!
Char(44): , => fine!
Char(45): - => fine!
Char(46): . => fine!
Char(47): / => fine!
Char(48): 0 => fine!
Char(49): 1 => fine!
Char(50): 2 => fine!
Char(51): 3 => fine!
Char(52): 4 => fine!
Char(53): 5 => fine!
Char(54): 6 => fine!
Char(55): 7 => fine!
Char(56): 8 => fine!
Char(57): 9 => fine!
Char(58): : => fine!
Char(59): ; => fine!
Char(60): => error! The '<' character, hexadecimal value 0x3C, cannot be included in a name. Line 5, position 13.
Char(61): = => fine!
Char(62): > => fine!
Char(63): ? => fine!
Char(64): @ => fine!
Char(65): A => fine!
Char(66): B => fine!
Char(67): C => fine!
Char(68): D => fine!
Char(69): E => fine!
Char(70): F => fine!
Char(71): G => fine!
Char(72): H => fine!
Char(73): I => fine!
Char(74): J => fine!
Char(75): K => fine!
Char(76): L => fine!
Char(77): M => fine!
Char(78): N => fine!
Char(79): O => fine!
Char(80): P => fine!
Char(81): Q => fine!
Char(82): R => fine!
Char(83): S => fine!
Char(84): T => fine!
Char(85): U => fine!
Char(86): V => fine!
Char(87): W => fine!
Char(88): X => fine!
Char(89): Y => fine!
Char(90): Z => fine!
Char(91): [ => fine!
Char(92): \ => fine!
Char(93): ] => fine!
Char(94): ^ => fine!
Char(95): _ => fine!
Char(96): ` => fine!
Char(97): a => fine!
Char(98): b => fine!
Char(99): c => fine!
Char(100): d => fine!
Char(101): e => fine!
Char(102): f => fine!
Char(103): g => fine!
Char(104): h => fine!
Char(105): i => fine!
Char(106): j => fine!
Char(107): k => fine!
Char(108): l => fine!
Char(109): m => fine!
Char(110): n => fine!
Char(111): o => fine!
Char(112): p => fine!
Char(113): q => fine!
Char(114): r => fine!
Char(115): s => fine!
Char(116): t => fine!
Char(117): u => fine!
Char(118): v => fine!
Char(119): w => fine!
Char(120): x => fine!
Char(121): y => fine!
Char(122): z => fine!
Char(123): { => fine!
Char(124): | => fine!
Char(125): } => fine!
Char(126): ~ => fine!
Char(127): => fine!

你可以看到有很多不符合XML代码的符号。要替换它们,我们可以使用Reqex.Replace

static string ReplaceHexadecimalSymbols(string txt)
{
string r = "[\x00-\x08\x0B\x0C\x0E-\x1F\x26]";
return Regex.Replace(txt, r,"",RegexOptions.Compiled);
}

C#十六进制值0x12,是一个无效字符 - 程序园的更多相关文章

  1. [No0000133]c# ERROR:“.”(十六进制值 0x00)是无效的字符

    December 24, 2013 c# exception “.”(十六进制值 0x00)是无效的字符. 解决方法:常见于socket方式连接(包括HTTP方式)等.报文交互时候,对方会在字符串后面 ...

  2. XML 十六进制值 是无效的字符错误 解决方法之一 转

    /// <summary> /// 过滤非打印字符 /// </summary> /// <param name="tmp">待过滤</p ...

  3. 调用webservice接口,报错:(十六进制值0x01)是无效的字符

    #事故现场 调用webservice接口,报错:(十六进制值0x01)是无效的字符. 如图: 意思是webservice返回的信息中包含无效的字符,无法解析成xml: #分析 使用postman向we ...

  4. (C++)读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔)

    1 /* 2 程序功能:读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔). 3 例如:当输入985这个数字时,显示如下信息: 4 985是一个3位数字! ...

  5. MySql_十六进制值

    十六进制值 MySQL支持十六进制值.在数字上下文中,十六进制数如同整数(64位精度).在字符串上下文,如同二进制字符串,每对十六进制数字被转换为一个字符: mysql> SELECT x'4D ...

  6. ibatis 参数错误,无效字符

    --- The error occurred in EmptyMapping.xml. --- The error occurred while applying a parameter map. - ...

  7. windows无效字符名导致的错误及解决办法

    今天用file_put_content($fileName,$data)产生错误:内容如下: Warning: file_put_contents(images/7d5636992a7395f9174 ...

  8. ORA-00911:无效字符错误

    ORA-00911:无效字符错误--造成构建环境的一个小错误 实施某功能脚本语句.编译时,出现了ORA-00911错误,当时有些疑惑,之前生产库使用是没有问题的,经过一番检查后发现原来是一个非常细微的 ...

  9. 解决错误:此用户名包含无效字符,请输入有效的用户名。wordpress不能注册中文用户名的问题

    wordpress在默认情况下不支持中文用户名,就是在后台添加用户的时候,如果用户名包含中文,则显示”错误:此用户名包含无效字符,请输入有效的用户名.”如何解决这个问题呢? 不用插件的话就需要修改一个 ...

随机推荐

  1. UVA11988_Broken Keyboard (a.k.a. Beiju Text)

    即将dfs()放到打印本段字符的后面 不过汝佳书上面说是用链表写的,无意中用递归写出来了,而且写的挺简单的,代码不复杂,写这个博客主要是想记住递归这种神奇的方法 平时递归搜索时候,dfs()的在其他代 ...

  2. 安装node --- 与升级

    1.安装node流程,请参照菜鸟教程的安装教程   http://www.runoob.com/nodejs/nodejs-install-setup.html 2.这里说一下踩的坑, 问题一.Win ...

  3. 详解 Flexible Box 中的 flex 属性

    导读: 弹性盒子是 CSS3 的一种布局模式,一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有适当的行为的布局方式.其中 flex 属性用于指定弹性子元素如何分配空间. flex 属性的值 ...

  4. POJ 1052 MPI Maelstrom

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5547   Accepted: 3458 Des ...

  5. JQuery 全选 反选 获取Table 中指定td的元素值

    //全选 function initTableCheckbox() { var $thr = $('table thead tr'); var $checkAllTh = $('<th>& ...

  6. dns轮训python

    环境 python3 先安装dnspython模块 httpclient模块 resolver模块 pip install dnspython pip install hhtpclient pip i ...

  7. 第一章 笔记本电脑安装Linux系统(Centos7)

    目标:通过[Linux+Docke+Nginx+Jenkins+k8s(Kubernetes)+CICD(自动化)]进行项目部署 内容:根据个人进度实时分章节记录自己所遇到的问题 一.准备工作 1.下 ...

  8. 开机流程 模块管理 Loader

    主机系统开机流程 boot loader 主要功能 显示核心模块加载信息 查询模块信息 核心模块的加载与移除 boot loader 的两个 stage grup2配置文件  /boot/grub2/ ...

  9. 前端学习(十七)js数组(笔记)

    数组:        1.    var arr=[1,2,3,4]; 2.    var arr=new Array(1,2,3,4,5); 区别:        1.写法不一样,选择第一种(好写) ...

  10. 转帖 eclipse Web项目WebContent目录修改

    最近在做Web 项目时,新建了一个WEB 项目,如webdemo,eclipse默认的build路径为build,WEB-INF存放于WebContent下面,今改了一个build路径和WebCont ...