一些代码的锦集
some good code from learning material
public void CopyTo(TransformerData[] array, int index)
{
for (IEnumerator e = this.GetEnumerator(); e.MoveNext(); )
{
array.SetValue(e.Current, index++);
}
}
~KeyAlgorithmPair()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
string host = Request.Url.Host;
if(!Request.Url.IsDefaultPort)
{
host += ":" + Request.Url.Port.ToString();
}派生deriving
public static bool IsNumeric(string text)
{
return Regex.IsMatch(text,"^\\d+$");
}StringDictionary
[XmlArray("HttpHandlers")] [XmlAttribute("defaultPageLocation")] [XmlIgnoreAttribute] lock Invoke
#region javasript
public static void ShowWindow(ref System.Web.UI.Page pgeParent,string strURL)
{
string o_strScript = "<script language=javascript>window.open('" + strURL + "','NewWin', 'location=yes scrollbars=yes menubar=yes status=yes resizable=1');";
o_strScript += (strURL + " </script>");
pgeParent.RegisterStartupScript("ShowWindow", o_strScript);
}

public static void ShowModalDialog(ref System.Web.UI.Page pgeParent,string strURL,string width,string height)
{
string o_strScript = "<script language=javascript>showModalDialog('{0}',window,'dialogWidth:{1};dialogHeight:{2};resizable:1;location=yes;scrollbars=yes;menubar=yes;status=yes;');";
o_strScript=string.Format(o_strScript,strURL,width,height);
o_strScript += (strURL + " </script>");
pgeParent.RegisterStartupScript("showModalDialog", o_strScript);
}

#endregion
private static bool IsEmptyString( string str)
{
if (str != null )
{
return (0 == str.Length);
}
return true;
}
#region Regex

public static string FilterScript(string content)
{
if(content==null || content=="")
{
return content;
}
string regexstr=@"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";//@"<script.*</script>";
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,"<script([^>])*>",string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,"</script>",string.Empty,RegexOptions.IgnoreCase);
}

public static string FilterIFrame(string content)
{
if(content==null || content=="")
{
return content;
}
string regexstr=@"(?i)<iframe([^>])*>(\w|\W)*</iframe([^>])*>";//@"<script.*</script>";
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,"<iframe([^>])*>",string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,"</iframe>",string.Empty,RegexOptions.IgnoreCase);
}

public static string CheckHtml(string content)
{
string regexfont1=@"(?i)<font([^>])*>(\w|\W)*</font([^>])*>";//@"<script.*</script>";
string regexfont2=@"<font([^>])*>";
if(Regex.IsMatch(content,regexfont2)&&(!Regex.IsMatch(content,regexfont1)))
{
return Regex.Replace(content,regexfont2,string.Empty,RegexOptions.IgnoreCase);
}
return content;
}

public static string RemoveHtml(string content)
{
string newstr=FilterScript(content);
string regexstr=@"<[^>]*>";
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}

public static string RemoveHtmlTag(string content,string[] tags)
{
string regexstr1,regexstr2;
foreach(string tag in tags)
{
if(tag!="")
{
regexstr1=string.Format(@"<{0}([^>])*>",tag);
regexstr2=string.Format(@"</{0}([^>])*>",tag);
content=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,regexstr2,string.Empty,RegexOptions.IgnoreCase);
}
}
return content;

}

public static string RemoveHtmlTag(string content,string tag)
{
string returnStr;
string regexstr1=string.Format(@"<{0}([^>])*>",tag);
string regexstr2=string.Format(@"</{0}([^>])*>",tag);
returnStr=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
returnStr=Regex.Replace(returnStr,regexstr2,string.Empty,RegexOptions.IgnoreCase);
return returnStr;

}

public static string ReplaceSpace(string content)
{
string findstr="(?<fore>(?:(?:[^< ])*(?:<(?:!--(?:(?:[^-])*(?:(?=-->)|-))*--|(?:[^>])+)>)?)*)[ ](?<back>(?:(?:[^< ])*(?:<(?:!--(?:(?:[^-])*(?:(?=-->)|-))*--|(?:[^>])+)>)?)*)";
//"(?<fore>(?:[^< ]*(?:<[^>]+>)?)*)[ ](?<back>(?:[^< ]*(?:<[^>]+>)?)*)";
string replacestr="${fore} ${back}";
string targetstr=System.Text.RegularExpressions.Regex.Replace(content,findstr,replacestr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return targetstr;

}

public static string[] CatchHtmlBlock(string content,string tag)
{
string findstr=string.Format(@"(?i)<{0}([^>])*>(\w|\W)*</{1}([^>])*>",tag,tag);
System.Text.RegularExpressions.MatchCollection matchs=System.Text.RegularExpressions.Regex.Matches(content,findstr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string[] strArray=new string[matchs.Count];
for(int i=0;i<strArray.Length;i++)
{
strArray[i]=matchs[i].Value;
}
return strArray;
}

public static bool IsNumeric(string text)
{
return Regex.IsMatch(text,"^\\d+$");
}

#endregion
public void CopyTo(TransformerData[] array, int index)
{
for (IEnumerator e = this.GetEnumerator(); e.MoveNext(); )
{
array.SetValue(e.Current, index++);
}
}
~KeyAlgorithmPair()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
string host = Request.Url.Host;
if(!Request.Url.IsDefaultPort)
{
host += ":" + Request.Url.Port.ToString();
}
public static bool IsNumeric(string text)
{
return Regex.IsMatch(text,"^\\d+$");
}[XmlArray("HttpHandlers")] [XmlAttribute("defaultPageLocation")] [XmlIgnoreAttribute] lock Invoke
#region javasript
public static void ShowWindow(ref System.Web.UI.Page pgeParent,string strURL)
{
string o_strScript = "<script language=javascript>window.open('" + strURL + "','NewWin', 'location=yes scrollbars=yes menubar=yes status=yes resizable=1');";
o_strScript += (strURL + " </script>");
pgeParent.RegisterStartupScript("ShowWindow", o_strScript);
}
public static void ShowModalDialog(ref System.Web.UI.Page pgeParent,string strURL,string width,string height)
{
string o_strScript = "<script language=javascript>showModalDialog('{0}',window,'dialogWidth:{1};dialogHeight:{2};resizable:1;location=yes;scrollbars=yes;menubar=yes;status=yes;');";
o_strScript=string.Format(o_strScript,strURL,width,height);
o_strScript += (strURL + " </script>");
pgeParent.RegisterStartupScript("showModalDialog", o_strScript);
}
#endregion 1
public class ExtendedProperties
2
{
3
private NameValueCollection _nvc;
4
5
public ExtendedProperties():this(new NameValueCollection())
6
{
7
}
8
9
public ExtendedProperties(NameValueCollection nvc)
10
{
11
_nvc = nvc;
12
}
13
14
public ExtendedProperties(byte[] bytes):this((NameValueCollection)BinarySerializer.Deserializer(bytes))
15
{
16
}
17
18
public byte[] Bytes
19
{
20
get
21
{
22
return BinarySerializer.Serialize(_nvc);
23
}
24
}
25
26
public string this[string key]
27
{
28
get{ return Get(key);}
29
set { Set(key,value);}
30
}
31
32
public string Get(string key)
33
{
34
return _nvc[key];
35
}
36
37
public void Set(string key, string text)
38
{
39
_nvc[key] = text;
40
}
41
}
42
public class BinarySerializer
43
{
44
private BinarySerializer()
45
{
46
}
47
48
public static byte[] Serialize(object value)
49
{
50
BinaryFormatter binaryFormatter = new BinaryFormatter();
51
MemoryStream ms = new MemoryStream();
52
byte[] b;
53
54
// Serialize the SiteSettings
55
binaryFormatter.Serialize(ms, value);
56
57
// Set the position of the MemoryStream back to 0
58
ms.Position = 0;
59
60
// Read in the byte array
61
b = new Byte[ms.Length];
62
ms.Read(b, 0, b.Length);
63
ms.Close();
64
65
return b;
66
}
67
68
public static object Deserializer(byte[] serializedExtendedAttributes)
69
{
70
if (serializedExtendedAttributes.Length == 0)
71
{
72
return null;
73
}
74
75
BinaryFormatter binaryFormatter = new BinaryFormatter();
76
MemoryStream ms = new MemoryStream();
77
try
78
{
79
ms.Write(serializedExtendedAttributes, 0, serializedExtendedAttributes.Length);
80
81
ms.Position = 0;
82
83
return binaryFormatter.Deserialize(ms);
84
}
85
finally
86
{
87
ms.Close();
88
}
89
}
90
}
public class ExtendedProperties2
{3
private NameValueCollection _nvc;4

5
public ExtendedProperties():this(new NameValueCollection())6
{7
}8

9
public ExtendedProperties(NameValueCollection nvc)10
{11
_nvc = nvc;12
}13

14
public ExtendedProperties(byte[] bytes):this((NameValueCollection)BinarySerializer.Deserializer(bytes))15
{16
}17

18
public byte[] Bytes19
{20
get21
{22
return BinarySerializer.Serialize(_nvc);23
}24
}25

26
public string this[string key]27
{28
get{ return Get(key);}29
set { Set(key,value);}30
}31

32
public string Get(string key)33
{34
return _nvc[key];35
}36

37
public void Set(string key, string text)38
{39
_nvc[key] = text;40
}41
}42
public class BinarySerializer43
{44
private BinarySerializer()45
{46
}47

48
public static byte[] Serialize(object value) 49
{50
BinaryFormatter binaryFormatter = new BinaryFormatter();51
MemoryStream ms = new MemoryStream();52
byte[] b;53

54
// Serialize the SiteSettings55
binaryFormatter.Serialize(ms, value);56

57
// Set the position of the MemoryStream back to 058
ms.Position = 0;59
60
// Read in the byte array61
b = new Byte[ms.Length];62
ms.Read(b, 0, b.Length);63
ms.Close();64

65
return b;66
}67

68
public static object Deserializer(byte[] serializedExtendedAttributes) 69
{70
if (serializedExtendedAttributes.Length == 0)71
{72
return null;73
}74

75
BinaryFormatter binaryFormatter = new BinaryFormatter();76
MemoryStream ms = new MemoryStream();77
try 78
{79
ms.Write(serializedExtendedAttributes, 0, serializedExtendedAttributes.Length);80

81
ms.Position = 0;82

83
return binaryFormatter.Deserialize(ms);84
} 85
finally86
{87
ms.Close();88
} 89
}90
}
private static bool IsEmptyString( string str)
{
if (str != null )
{
return (0 == str.Length);
}
return true;
}
#region Regex
public static string FilterScript(string content)
{
if(content==null || content=="")
{
return content;
}
string regexstr=@"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";//@"<script.*</script>";
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,"<script([^>])*>",string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,"</script>",string.Empty,RegexOptions.IgnoreCase);
}
public static string FilterIFrame(string content)
{
if(content==null || content=="")
{
return content;
}
string regexstr=@"(?i)<iframe([^>])*>(\w|\W)*</iframe([^>])*>";//@"<script.*</script>";
content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,"<iframe([^>])*>",string.Empty,RegexOptions.IgnoreCase);
return Regex.Replace(content,"</iframe>",string.Empty,RegexOptions.IgnoreCase);
}
public static string CheckHtml(string content)
{
string regexfont1=@"(?i)<font([^>])*>(\w|\W)*</font([^>])*>";//@"<script.*</script>";
string regexfont2=@"<font([^>])*>";
if(Regex.IsMatch(content,regexfont2)&&(!Regex.IsMatch(content,regexfont1)))
{
return Regex.Replace(content,regexfont2,string.Empty,RegexOptions.IgnoreCase);
}
return content;
}
public static string RemoveHtml(string content)
{
string newstr=FilterScript(content);
string regexstr=@"<[^>]*>";
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}
public static string RemoveHtmlTag(string content,string[] tags)
{
string regexstr1,regexstr2;
foreach(string tag in tags)
{
if(tag!="")
{
regexstr1=string.Format(@"<{0}([^>])*>",tag);
regexstr2=string.Format(@"</{0}([^>])*>",tag);
content=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
content=Regex.Replace(content,regexstr2,string.Empty,RegexOptions.IgnoreCase);
}
}
return content;
}
public static string RemoveHtmlTag(string content,string tag)
{
string returnStr;
string regexstr1=string.Format(@"<{0}([^>])*>",tag);
string regexstr2=string.Format(@"</{0}([^>])*>",tag);
returnStr=Regex.Replace(content,regexstr1,string.Empty,RegexOptions.IgnoreCase);
returnStr=Regex.Replace(returnStr,regexstr2,string.Empty,RegexOptions.IgnoreCase);
return returnStr;
}
public static string ReplaceSpace(string content)
{
string findstr="(?<fore>(?:(?:[^< ])*(?:<(?:!--(?:(?:[^-])*(?:(?=-->)|-))*--|(?:[^>])+)>)?)*)[ ](?<back>(?:(?:[^< ])*(?:<(?:!--(?:(?:[^-])*(?:(?=-->)|-))*--|(?:[^>])+)>)?)*)";
//"(?<fore>(?:[^< ]*(?:<[^>]+>)?)*)[ ](?<back>(?:[^< ]*(?:<[^>]+>)?)*)";
string replacestr="${fore} ${back}";
string targetstr=System.Text.RegularExpressions.Regex.Replace(content,findstr,replacestr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return targetstr;
}
public static string[] CatchHtmlBlock(string content,string tag)
{
string findstr=string.Format(@"(?i)<{0}([^>])*>(\w|\W)*</{1}([^>])*>",tag,tag);
System.Text.RegularExpressions.MatchCollection matchs=System.Text.RegularExpressions.Regex.Matches(content,findstr,System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string[] strArray=new string[matchs.Count];
for(int i=0;i<strArray.Length;i++)
{
strArray[i]=matchs[i].Value;
}
return strArray;
}
public static bool IsNumeric(string text)
{
return Regex.IsMatch(text,"^\\d+$");
}
#endregion


浙公网安备 33010602011771号