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

XML异常处理技巧
本文介绍了解决XML加载或保存过程中因包含无效低位非打印字符而引发的System.ArgumentException异常的方法。通过提供具体的代码示例,展示了如何在生成XML文件时过滤这些无效字符,确保XML文档的合规性和正确性。

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

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

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



 

        /// <summary>
        /// 把一个字符串中的 低序位 ASCII 字符 替换成   字符
        /// 转换  ASCII  0 - 8  -> � - 
        /// 转换  ASCII 11 - 12 ->  - 
        /// 转换  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>
        /// 把一个字符串中的下列字符替换成 低序位 ASCII 字符
        /// 转换  � -   -> ASCII  0 - 8
        /// 转换   -   -> ASCII 11 - 12
        /// 转换   -  -> 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;
                string rOldV = string.Empty, rNewV = string.Empty;
                int le = (len - pos < 6) ? len - pos : 6;
                int p = input.IndexOf(";", pos, le);
                if (p >= 0)
                {
                    rOldV = input.Substring(pos, p - pos + 1);
                    // 计算 对应的低位字符
                    short ss;
                    if (short.TryParse(rOldV.Substring(3, p - pos - 3), System.Globalization.NumberStyles.AllowHexSpecifier, null, out ss))
                    {
                        if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
                        {
                            needReplace = true;
                            rNewV = Convert.ToChar(ss).ToString();
                        }
                    }
                    pos = p + 1;
                }
                else pos += le;
                string part = input.Substring(startIndex, pos - startIndex);
                if (needReplace) result.Append(part.Replace(rOldV, rNewV));
                else result.Append(part);
                startIndex = pos;
            }
            result.Append(input.Substring(startIndex));
            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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值