Skip to content

Instantly share code, notes, and snippets.

@jdowd7
jdowd7 / EscapedAmpersand.cs
Created October 30, 2015 18:05
Check for XML encoded chars
public static ValidationResult EscapedAmpersand(string sampleStr)
{
Regex specChar = new Regex(@"&(?!(?:apos|quot|[A-Za-z0-1!?.,\ -]|amp);|#)", RegexOptions.IgnoreCase);
MatchCollection matches = specChar.Matches(sampleStr);
if (matches.Count > 0)
{
return new ValidationResult("Message cannot contain special characters.");
}
return ValidationResult.Success;
@jdowd7
jdowd7 / containsSpecialCharacterFunct.cs
Created October 30, 2015 18:03
Checks to see if a string contains a special character
public static ValidationResult ContainsSpecChar(string sampleStr)
{
Regex specChar = new Regex(@"[^A-Za-z0-9!?.&,\ -]", RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = specChar.Matches(sampleStr);
if (matches.Count > 0)
{
return new ValidationResult("Message cannot contain special characters.");
}
return ValidationResult.Success;
@jdowd7
jdowd7 / containsUrlFunction.cs
Created October 30, 2015 18:02
Checks if a string contains a URL
public static ValidationResult ContainsUrl(string sampleStr)
{
if(Uri.IsWellFormedUriString(sampleStr, UriKind.Absolute))
{
return new ValidationResult("Message Contains a URL.");
}
Regex urlRx = new Regex(@"(?<url>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)", RegexOptions.IgnoreCase);
MatchCollection matches = urlRx.Matches(sampleStr);
@jdowd7
jdowd7 / convertsDatatableToXML.cs
Created August 14, 2015 14:08
Converts any datatable to serializable XML
private string ConvertDatabaseToXML(DataTable dt)
{
System.IO.MemoryStream memory = new System.IO.MemoryStream();
dt.WriteXml(memory, true);
memory.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamReader memReader = new System.IO.StreamReader(memory);
string xmlResults;
xmlResults = memReader.ReadToEnd();
return xmlResults;
@jdowd7
jdowd7 / dataTableEnforceNulls.cs
Created August 14, 2015 14:07
Enforces null columns to prevent getting dropped when serializing
private static void datatableEnforceNulls(DataTable resultTable)
{
List<string> removeList = new List<string>();
foreach (DataColumn column in resultTable.Columns)
{
if (resultTable.Rows.OfType<DataRow>().All(r => r.IsNull(column)))
{
@jdowd7
jdowd7 / changeValueXmlNode
Last active August 29, 2015 14:01
change values within a xml node
//parse the XML, might as well use Xpath to avoid dupe root nodes.... ughhhhh
System.Xml.XPath.XPathDocument xmlResults = new System.Xml.XPath.XPathDocument(customObjectThatHasXmlData.Data.InnerXml.ToString());
System.Xml.XPath.XPathNavigator xmlResultsNav = xmlResults.CreateNavigator();
System.Xml.XPath.XPathExpression xmlResultsExpression;
xmlResultsExpression = xmlResultsNav.Compile(@"/Data/result xmlns=""/time_stamp"); //in this case was trying to get the Tstamp
System.Xml.XPath.XPathNodeIterator xmlResultsNavIterator = xmlResultsNav.Select(xmlResultsExpression);
while(xmlResultsNavIterator.MoveNext())
{
@jdowd7
jdowd7 / RoundTime
Created May 13, 2014 15:04
Rounds time to :00 or :05, C#, roundtime
private DateTime RoundUp(DateTime dt, TimeSpan d)
{
return new DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);
}
@jdowd7
jdowd7 / genericObjectCompareClass
Created May 13, 2014 15:00
Just a generic class for comparison: C#, generic object, IEqualityComparer<generic>, multiple attribute object, hashcode of object
class genericObjectCompareClass : IEqualityComparer<objectType>
{
#region IEqualityComparer<objectType> Members
public bool Equals(Object1 x, Object2 y)
{
return x.Attribute1.Equals(y.Attribute1);
}
public int GetHashCode(vipFlip obj)