xml特殊字符处理

前端之家收集整理的这篇文章主要介绍了xml特殊字符处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

protected void Button1_Click(object sender,EventArgs e) 
        { 
            string strpageContent = string.Empty; 
            string strXML = string.Empty; 


            //从xml文件中读取内容 
            StreamReader reader = new StreamReader("D:\\SMS.xml",Encoding.GetEncoding("GB2312")); 

            strpageContent = reader.ReadToEnd(); 

            //正则表达式匹配Message 
            string regText = @"<Message>(.*)</Message>"; 

            //替换匹配结果 
            string xmlResult = Regex.Replace(strpageContent,regText,new MatchEvaluator(RefineMessage),RegexOptions.Multiline);

            ArrayOfSmsReceiveEntity arrayOfSmsReceiveEntity = ObjectToXml<ArrayOfSmsReceiveEntity>.Deserialize(xmlResult);

            List<SmsReceiveEntity> smsReceiveEntityList = new List<SmsReceiveEntity>(); 

            if (arrayOfSmsReceiveEntity != null && arrayOfSmsReceiveEntity.SmsReceiveEntity.Length > 0) 
            { 
                smsReceiveEntityList.AddRange(arrayOfSmsReceiveEntity.SmsReceiveEntity); 

                foreach (SmsReceiveEntity smsReceiveEntity in smsReceiveEntityList) 
                { 
                    string message = smsReceiveEntity.Message; 

                    //Base64解码,还原
                    byte[] bytes = Convert.FromBase64String(message); 

                    string decodeMessage = Encoding.GetEncoding("GB2312").GetString(bytes); 
                } 
            } 
        } 


        private string RefineMessage(Match match) 
        { 
            string message = match.ToString();   
            message = message.Replace("<Message>",""); 
            message = message.Replace("</Message>",""); 

            //对内容进行Base64编码,特殊字符转化为了Base64
            message = Convert.ToBase64String(Encoding.GetEncoding("GB2312").GetBytes(message)); 

            return string.Format("<Message>{0}</Message>",message); 
        }

1.string字提取出来,转化为base64,然后再进行xmlDocument加载处理。

2.解决方案是HtmlEnode(),服务端进行,客户端在显示时会自动HtmlDecode()

3.\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [\f\n\r\t\v]。




猜你在找的XML相关文章