private static bool ProcessRestrictions( string value, SchemaValidationResult errors, XmlSchemaSimpleTypeRestriction restriction, string typeName, string name) { bool isValid = true; if (restriction == null) return false; foreach (object facet in restriction.Facets) { if (facet is XmlSchemaMinInclusiveFacet) { var lf = facet as XmlSchemaMinInclusiveFacet; if (!string.IsNullOrEmpty( value )) { int iResult; double dResult; if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) && iResult < int.Parse( lf.Value ) ) || ( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) && dResult < double.Parse( lf.Value ) )) { errors.AddError( String.Format( "The value of {0} must not be less than {1}", name, lf.Value ) ); isValid = false; } } } else if (facet is XmlSchemaMinExclusiveFacet) { var lf = facet as XmlSchemaMinExclusiveFacet; if (!string.IsNullOrEmpty( value )) { int iResult; double dResult; if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) && iResult <= int.Parse( lf.Value ) ) || ( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) && dResult <= double.Parse( lf.Value ) )) { errors.AddError( String.Format( "The value of {0} must not be less than or equal to {1}", name, lf.Value ) ); isValid = false; } } } else if (facet is XmlSchemaMaxInclusiveFacet) { var lf = facet as XmlSchemaMaxInclusiveFacet; if (!string.IsNullOrEmpty( value )) { int iResult; double dResult; if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) && iResult > int.Parse( lf.Value ) ) || ( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) && dResult > double.Parse( lf.Value ) )) { errors.AddError( String.Format( "The value of {0} must not be greater than {1}", name, lf.Value ) ); isValid = false; } } } else if (facet is XmlSchemaMaxExclusiveFacet) { var lf = facet as XmlSchemaMaxExclusiveFacet; if (!string.IsNullOrEmpty( value )) { int iResult; double dResult; if (( typeName.Contains( "int" ) && int.TryParse( value, out iResult ) && iResult >= int.Parse( lf.Value ) ) || ( typeName.Contains( "dou" ) && double.TryParse( value, out dResult ) && dResult >= double.Parse( lf.Value ) )) { errors.AddError( String.Format( "The value of {0} must not be greater than or equal to {1}", name, lf.Value ) ); isValid = false; } } } else if (facet is XmlSchemaLengthFacet) { var lf = facet as XmlSchemaLengthFacet; if (!String.IsNullOrEmpty( value ) && value.Length > int.Parse( lf.Value )) { errors.AddError( String.Format( "The value's length must not excede {0} characters", lf.Value ) ); isValid = false; } } else if (facet is XmlSchemaMinLengthFacet) { var lf = facet as XmlSchemaMinLengthFacet; if (!String.IsNullOrEmpty( value ) && value.Length < int.Parse( lf.Value )) { errors.AddError( String.Format( "The value's length must not be less than {0} characters", lf.Value ) ); isValid = false; } } else if (facet is XmlSchemaMaxLengthFacet) { var lf = facet as XmlSchemaMaxLengthFacet; if (!String.IsNullOrEmpty( value ) && value.Length > int.Parse( lf.Value )) { errors.AddError( String.Format( "The value's length must not be greater than {0} characters", lf.Value ) ); isValid = false; } } else if (facet is XmlSchemaPatternFacet) { var spf = facet as XmlSchemaPatternFacet; Console.WriteLine( spf.Id + @" : " + spf.Value ); if (value != null && !Regex.Match( value, spf.Value ).Success) { errors.AddError( String.Format( "The \"{0}\" value must match the regular expression: {1}", name, spf.Value ) ); isValid = false; } } else if (facet is XmlSchemaEnumerationFacet) { //TODO:XmlSchemaEnumerationFacet int i = 0; } else if (facet is XmlSchemaTotalDigitsFacet) { //TODO:XmlSchemaTotalDigitsFacet int i = 0; } else if (facet is XmlSchemaWhiteSpaceFacet) { //TODO:XmlSchemaWhiteSpaceFacet int i = 0; } } return isValid; }
public static bool ValidateAttribute( XmlSchemaAttribute attribute, string value, SchemaValidationResult errors ) { bool isValid = true; XmlSchemaSimpleType attributeSchemaType = attribute.AttributeSchemaType; XmlSchemaUse use = attribute.Use; string name = attribute.Name; bool required = ( use == XmlSchemaUse.Required ); if (required && string.IsNullOrEmpty( value )) { isValid = false; errors.AddError( string.Format( "{0} is required\n", name ) ); } if (attributeSchemaType.Content is XmlSchemaSimpleTypeRestriction) { XmlQualifiedName qname = attributeSchemaType.BaseXmlSchemaType.QualifiedName; String typeName = qname.Name; var restriction = (XmlSchemaSimpleTypeRestriction) attributeSchemaType.Content; isValid &= ProcessRestrictions( value, errors, restriction, typeName, name ); } return isValid; }
public static bool ValidateElement( XmlSchemaElement element, object value, SchemaValidationResult errors ) { bool isValid = true; XmlSchemaType schemaType = element.ElementSchemaType; bool isNillable = element.IsNillable; string defaultValue = element.DefaultValue; decimal minOccurrs = element.MinOccurs; decimal maxOccurrs = element.MaxOccurs; //SchemaValidationResult errors = new SchemaValidationResult(element.Name ); var complexType = schemaType as XmlSchemaComplexType; var simpleType = schemaType as XmlSchemaSimpleType; if (minOccurrs > 0 && value == null && !isNillable) { isValid = false; errors.AddError( string.Format( "{0} cannot be null", element.Name ) ); } else if (complexType != null) { var collection = value as ICollection; if (collection != null) { XmlSchemaParticle contentTypeParticle = complexType.ContentTypeParticle; XmlSchemaParticle particle = complexType.Particle; XmlSchemaContentModel model = complexType.ContentModel; var sequence = particle as XmlSchemaSequence; if (sequence != null) { ProcessSequence( errors, sequence, collection, minOccurrs ); } } } else if (simpleType != null) { var union = simpleType.Content as XmlSchemaSimpleTypeUnion; var list = simpleType.Content as XmlSchemaSimpleTypeList; var restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction; if (restriction != null) { XmlQualifiedName qname = simpleType.BaseXmlSchemaType.QualifiedName; String typeName = qname.Name; isValid &= ProcessRestrictions( value == null ? null : value.ToString(), errors, restriction, typeName, simpleType.Name ); } foreach (XmlSchemaObject contraint in element.Constraints) { var key = contraint as XmlSchemaKey; var keyRef = contraint as XmlSchemaKeyref; var unique = contraint as XmlSchemaUnique; if (key != null) { } if (keyRef != null) { } if (unique != null) { } } } XmlTypeCode tc = schemaType.TypeCode; //validationResult.AddResult( errors ); return isValid; }
private static void ProcessSequence( SchemaValidationResult errors, XmlSchemaSequence sequence, ICollection collection, decimal minOccurrs) { foreach (XmlSchemaObject item in sequence.Items) { var se = item as XmlSchemaElement; var gr = item as XmlSchemaGroupRef; var sc = item as XmlSchemaChoice; var ss = item as XmlSchemaSequence; var sa = item as XmlSchemaAny; if (se != null) { decimal max = se.MaxOccurs; decimal min = se.MinOccurs; string maxString = se.MaxOccursString; string minString = se.MinOccursString; if (collection.Count > max) errors.AddError( string.Format( "{0} count may not be more than {1}", se.Name, string.IsNullOrEmpty( maxString ) ? "" + max : maxString ) ); if (collection.Count < min && minOccurrs > 0) errors.AddError( string.Format( "{0} count may not be less than {1}", se.Name, ( string.IsNullOrEmpty( minString ) ? "" + min : minString ) ) ); } else if (sc != null) { foreach (XmlSchemaObject scItem in sc.Items) { var scse = scItem as XmlSchemaElement; var scgr = scItem as XmlSchemaGroupRef; var scsc = scItem as XmlSchemaChoice; var scss = scItem as XmlSchemaSequence; var scsa = scItem as XmlSchemaAny; } } else if (ss != null) { //ProcessSequence(errors, ss, ICollection collection, } } }