Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 using System.Text;
00040
00041 namespace XMLStorage
00042 {
00043
00044
00045
00046
00047 public class XMLHelper
00048 {
00049
00050
00051
00052 private XMLHelper()
00053 {
00054 }
00055
00056
00057
00058
00059 public static string encodeXMLString(string s)
00060 {
00061 return encodeXMLString(s, false);
00062 }
00063
00064
00065
00066
00067 public static string encodeXMLString(string s, bool cdata)
00068 {
00069 StringBuilder buffer = new StringBuilder();
00070
00071 if (cdata)
00072 {
00073 buffer.Append("<![CDATA[");
00074 buffer.Append(s);
00075 buffer.Append("]]>");
00076 }
00077 else
00078 {
00079 foreach(char c in s)
00080 {
00081 switch (c)
00082 {
00083 case '<':
00084 buffer.Append("<");
00085 break;
00086
00087 case '>':
00088 buffer.Append(">");
00089 break;
00090
00091 case '&':
00092 buffer.Append("&");
00093 break;
00094
00095 case '"':
00096 buffer.Append(""");
00097 break;
00098
00099 case '\'':
00100 buffer.Append("'");
00101 break;
00102
00103 default:
00104 buffer.Append(c);
00105 break;
00106 }
00107 }
00108 }
00109
00110 return buffer.ToString();
00111 }
00112
00113
00114
00115
00116 public static string decodeXMLString(string s)
00117 {
00118 StringBuilder buffer = new StringBuilder();
00119
00120 for(int i = 0; i < s.Length; ++i)
00121 {
00122 char c = s[i];
00123
00124
00125 if (c == '&')
00126 {
00127 if (string.Compare(s, i+1, "lt;", 0, 3, true) == 0)
00128 {
00129 buffer.Append('<');
00130 i += 3;
00131 }
00132 if (string.Compare(s, i+1, "gt;", 0, 3, true) == 0)
00133 {
00134 buffer.Append('>');
00135 i += 3;
00136 }
00137 if (string.Compare(s, i+1, "amp;", 0, 4, true) == 0)
00138 {
00139 buffer.Append('&');
00140 i += 4;
00141 }
00142 if (string.Compare(s, i+1, "quot;", 0, 5, true) == 0)
00143 {
00144 buffer.Append('"');
00145 i += 5;
00146 }
00147 if (string.Compare(s, i+1, "apos;", 0, 5, true) == 0)
00148 {
00149 buffer.Append('\'');
00150 i += 5;
00151 }
00152 else
00153 buffer.Append(c);
00154 }
00155
00156 else if (c=='<' && string.Compare(s, i+1, "![CDATA[", 0, 8)==0)
00157 {
00158 int e = s.IndexOf("]]>", i+9);
00159 if (e != -1)
00160 {
00161 i += 9;
00162 int l = e - i;
00163 buffer.Append(s.Substring(i, l));
00164 i += 3;
00165 }
00166 else
00167 buffer.Append(c);
00168 }
00169 else
00170 buffer.Append(c);
00171 }
00172
00173 return buffer.ToString();
00174 }
00175
00176
00177 public static string unescape(string s)
00178 {
00179 return XMLHelper.unescape(s, '"', '"');
00180 }
00181
00182
00183 public static string unescape(string s, char b, char e)
00184 {
00185
00186
00187
00188
00189
00190
00191 if (s[0] == b)
00192 if (s.Length>1 && s[s.Length-1]==e)
00193 s = s.Substring(1, s.Length-2);
00194
00195 return s;
00196 }
00197 }
00198
00199 }