/// <summary> /// Cleans the value according to the field's data format and maximum length. /// </summary> /// <param name="field">the field</param> /// <param name="value">the value</param> /// <returns>the sanitized field</returns> protected static string CleanValue(RequestFieldNames field, string value) { value = value ?? string.Empty; // get rid of null value = value.Trim(); // could throw exception but we don't care about cropped addresses in their system because our system remembers the original string, except for the CC-related things. if (value.Length > MaxLengths[field]) { value = value.Substring(0, MaxLengths[field]); } if (NumericFields.Contains(field)) { if (!Regex.Match(value, @"^[0-9]{0," + MaxLengths[field] + "}$").Success) { throw new ArgumentException(String.Format("Field {0} was not a number of the proper length (value={1}).", GetField(field), value)); } } if (DollarFields.Contains(field)) { if (!("0" == value || Regex.Match(value, @"^[0-9]{0,6}\.[0-9]{0,2}$").Success)) { throw new ArgumentException(String.Format("Field {0} was not a dollar amount (value={1}).", GetField(field), value)); } } return(value); }
/// <summary> /// Adds a new child xml node to the transaction. /// </summary> /// <param name="xml">the xml request</param> /// <param name="transaction">the transaction in it</param> /// <param name="field">the field name to be added</param> /// <param name="value">the value to be added</param> protected static void AddChild(XmlDocument xml, XmlNode transaction, RequestFieldNames field, string value) { XmlNode n = xml.CreateNode(XmlNodeType.Element, GetField(field), string.Empty); value = CleanValue(field, value); n.InnerText = value; transaction.AppendChild(n); }
/// <summary> /// Given a field, return the field name. /// </summary> /// <param name="f">the field</param> /// <returns>the field name</returns> public static string GetField(RequestFieldNames f) { return(Enum.GetName(typeof(RequestFieldNames), f)); }