C#过滤XML(十六进制值 0x1D)无效的字符

本文介绍了解决XML加载或保存过程中出现的System.ArgumentException异常的方法。该异常通常由于XML文件中含有非法的低位非打印字符引起。文章提供了两种实用的C#代码示例来过滤这些非法字符,确保XML文件的有效性和正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加载或保存XML时引发的异常.System.ArgumentException: “”(十六进制值 0x1D)是无效的字符。
产生原因是xml文件中包含低位非打印字符造成的
处理方法:在产生xml文件的时候,过滤低位非打印字符

把一个字符串中的 低序位 ASCII 字符 替换成 &#x  字符
转换  ASCII  0 - 8  -> � -
转换  ASCII 11 - 12 -> - 
转换  ASCII 14 - 31 -> -

[html]   view plain copy
  1. return System.Text.RegularExpressions.Regex.Replace(HttpUtility.HtmlEncode(str),@"[\x00-\x08]|[\x0B-\x0C]|[\x0E-\x1F]", "");  



 

[csharp]   view plain copy
  1. /// <summary>  
  2. /// 把一个字符串中的 低序位 ASCII 字符 替换成   字符  
  3. /// 转换  ASCII  0 - 8  -> � -   
  4. /// 转换  ASCII 11 - 12 ->   -    
  5. /// 转换  ASCII 14 - 31 ->  -   
  6. /// </summary>  
  7. /// <param name="tmp"></param>  
  8. /// <returns></returns>  
  9. public static string ReplaceLowOrderASCIICharacters(string tmp)  
  10. {  
  11.     StringBuilder info = new StringBuilder();  
  12.     foreach (char cc in tmp)  
  13.     {  
  14.         int ss = (int)cc;  
  15.         if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))  
  16.             info.AppendFormat("{0:X};", ss);  
  17.         else info.Append(cc);  
  18.     }  
  19.     return info.ToString();  
  20. }  
  21. /// <summary>  
  22. /// 把一个字符串中的下列字符替换成 低序位 ASCII 字符  
  23. /// 转换  � -   -> ASCII  0 - 8  
  24. /// 转换    -    -> ASCII 11 - 12  
  25. /// 转换   -  -> ASCII 14 - 31  
  26. /// </summary>  
  27. /// <param name="input"></param>  
  28. /// <returns></returns>  
  29. public static string GetLowOrderASCIICharacters(string input)  
  30. {  
  31.     if (string.IsNullOrEmpty(input)) return string.Empty;  
  32.     int pos, startIndex = 0, len = input.Length;  
  33.     if (len <= 4) return input;  
  34.     StringBuilder result = new StringBuilder();  
  35.     while ((pos = input.IndexOf("", startIndex)) >= 0)  
  36.     {  
  37.         bool needReplace = false;  
  38.         string rOldV = string.Empty, rNewV = string.Empty;  
  39.         int le = (len - pos < 6) ? len - pos : 6;  
  40.         int p = input.IndexOf(";", pos, le);  
  41.         if (p >= 0)  
  42.         {  
  43.             rOldV = input.Substring(pos, p - pos + 1);  
  44.             // 计算 对应的低位字符  
  45.             short ss;  
  46.             if (short.TryParse(rOldV.Substring(3, p - pos - 3), System.Globalization.NumberStyles.AllowHexSpecifier, nullout ss))  
  47.             {  
  48.                 if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))  
  49.                 {  
  50.                     needReplace = true;  
  51.                     rNewV = Convert.ToChar(ss).ToString();  
  52.                 }  
  53.             }  
  54.             pos = p + 1;  
  55.         }  
  56.         else pos += le;  
  57.         string part = input.Substring(startIndex, pos - startIndex);  
  58.         if (needReplace) result.Append(part.Replace(rOldV, rNewV));  
  59.         else result.Append(part);  
  60.         startIndex = pos;  
  61.     }  
  62.     result.Append(input.Substring(startIndex));  
  63.     return result.ToString();  

<?xml version="1.0" encoding="utf-8" ?> <Information> <!-- 哑铃型连杆1 弯头型2 平头型3 弯头无箱型4 --> <LinkageType> 1 </LinkageType> <PLMID>Linkage_PLID_001</PLMID> <Parts> <Part> <PLMID>Part_PLID_001</PLMID> <!-- 左加强板1 右加强板2 盖板3 底板4 --> <Type>1</Type> <Points> <Point id="1"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P1</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,-1140,195.8</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,-1101,195.8</EndPoint> <!-- 焊角工艺标识,双方约定的标识符,如L8、V20,代号与郑煤机焊接工艺表一致 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="2"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P2</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,-1140,-193.2</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,-1098,-19.61</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="3"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P3</Position> <!-- 直线段1 圆弧2 --> <LineType>2</LineType> <!-- 起点 --> <StartPoint>40,-668.5,57.27</StartPoint> <!-- 中间点 --> <MiddlePoint>-675.3,0,40</MiddlePoint> <!-- 终点 --> <EndPoint>40,-668.5,-56.82</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="4"> <!-- 点固1 预埋2 --> <Function>2</Function> <!-- 位置?根据点位确定姿态 --> <Position>P456</Position> <!-- 直线段1 圆弧2 --> <LineType>2</LineType> <!-- 起点 --> <StartPoint>40,-641.6,111.9</StartPoint> <!-- 中间点 --> <MiddlePoint>40,-675.9,0</MiddlePoint> <!-- 终点 --> <EndPoint>40,-642.2,110.8</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> </Points> </Part> <Part> <PLMID>Part_PLID_002</PLMID> <!-- 左加强板1 右加强板2 盖板3 底板4 --> <Type>2</Type> <Points> <Point id="1"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P1</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,1097,196.8</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,1140,196.8</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="2"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P2</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,1139,-195.4</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,1097,-195.4</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="3"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P3</Position> <!-- 直线段1 圆弧2 --> <LineType>2</LineType> <!-- 起点 --> <StartPoint>40,667,56.35</StartPoint> <!-- 中间点 --> <MiddlePoint>40,675,0</MiddlePoint> <!-- 终点 --> <EndPoint>40,667.9,-54.94</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="4"> <!-- 点固1 预埋2 --> <Function>2</Function> <!-- 位置?根据点位确定姿态 --> <Position>P456</Position> <!-- 直线段1 圆弧2 --> <LineType>2</LineType> <!-- 起点 --> <StartPoint>40,640.5,112</StartPoint> <!-- 中间点 --> <MiddlePoint>40,675,0</MiddlePoint> <!-- 终点 --> <EndPoint>40,640.5,-112.4</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> </Points> </Part> <Part> <PLMID>Part_PLID_003</PLMID> <!-- 左加强板1 右加强板2 盖板3 底板4 --> <Type>3</Type> <Points> <Point id="1"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P1</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>90,-1194,148.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>90,-1144,178.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="2"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P2</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,-633.8,146.3</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,-583.8,146.3</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="3"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P3</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,583.1,146.5</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,633.1,146.5</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="4"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P4</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>90,1144,178.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>90,1194,148.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="5"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P5</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>305.2,-1194,148.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>305.2,-1144,178.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="6"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P6</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>355.2,-633.8,146.3</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>355.2,-583.8,146.3</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="7"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P7</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>355.2,583.1,146.5</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>355.2,633.1,146.5</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="8"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P8</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>305.2,1144,178.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>305.2,1194,148.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> </Points> </Part> <Part> <PLMID>Part_PLID_004</PLMID> <!-- 左加强板1 右加强板2 盖板3 底板4 --> <Type>4</Type> <Points> <Point id="1"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P1</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>90,-1194,-148.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>90,-1144,-178.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="2"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P2</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,-633.8,-146.3</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,-583.8,-146.3</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="3"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P3</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>40,583.1,-146.5</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>40,633.1,-146.5</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="4"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P4</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>90,1144,-178.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>90,1194,-148.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="5"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P5</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>305.2,-1194,-148.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>305.2,-1144,-178.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="6"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P6</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>355.2,-633.8,-146.3</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>355.2,-583.8,-146.3</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="7"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P7</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>355.2,583.1,-146.5</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>355.2,633.1,-146.5</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> <Point id="8"> <!-- 点固1 预埋2 --> <Function>1</Function> <!-- 位置?根据点位确定姿态 --> <Position>P8</Position> <!-- 直线段1 圆弧2 --> <LineType>1</LineType> <!-- 起点 --> <StartPoint>305.2,1144,-178.6</StartPoint> <!-- 中间点 --> <MiddlePoint></MiddlePoint> <!-- 终点 --> <EndPoint>305.2,1194,-148.6</EndPoint> <!-- 焊角工艺标识,双方约定的标识符 --> <WeldingMark>V20</WeldingMark> </Point> </Points> </Part> </Parts> </Information> 参考文件
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值