XML中特殊字符转义

 1 /// <summary>
 2 /// 特殊字符转义
 3 /// </summary>
 4 /// <param name="xml"></param>
 5 /// <returns></returns>
 6 public string XmlCharacterEscaping(string xml)
 7 {
 8     string xmlNew = xml;
 9     if (xml.IndexOf("&") != -1)
10     {
11         xmlNew = xml.Replace("&", "&amp;");
12     }
13     if (xml.IndexOf("<") != -1)
14     {
15         xmlNew = xml.Replace("<", "&lt;");
16     }
17     if (xml.IndexOf(">") != -1)
18     {
19         xmlNew = xml.Replace(">", "&gt;");
20     }
21     if (xml.IndexOf('"') != -1)
22     {
23         char c = '"';
24         xmlNew = xml.Replace(c.ToString(), "&quot;");
25     }
26 return xmlNew; 27 }

 

posted @ 2016-12-29 10:45  wuln  阅读(592)  评论(0编辑  收藏  举报