/// <summary> /// Метод валидации XML-данных по XSD-схеме. /// </summary> /// <param name="fileName">XML-файл.</param> /// <param name="schemaFile">XSD-файл.</param> /// <returns></returns> public static ValidationStatus ValidateXML(string fileName, string schemaFile) { ValidationStatus = ValidationStatus.Process; try { XmlSchemaSet schema = new XmlSchemaSet(); schema.Add(null, schemaFile); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessIdentityConstraints; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.Schemas = schema; settings.ValidationEventHandler += ValidationCallBack; XmlReader reader = XmlReader.Create(fileName, settings); while (reader.Read()) { ; } } catch (Exception ex) { ValidationStatus = ValidationStatus.Denied; } if (!ValidationStatus.Equals(ValidationStatus.Denied)) { ValidationStatus = ValidationStatus.Passed; } return(ValidationStatus); }
/// <summary> /// Sets error messages into observable dictionary. /// </summary> /// <param name="status">Result of validation</param> /// <param name="errors">Dictionary containing error messages.</param> private static void FillErrors(ValidationStatus status, string key, ValidationType type, ref ObservableDictionary <string, string> errors) { if (status.Equals(ValidationStatus.Ok)) { return; } if (status.HasFlag(ValidationStatus.Empty)) { errors[key] = Constants.FieldIsRequired; } else if (status.HasFlag(ValidationStatus.LoginInvalid)) { errors[key] = Constants.LoginInvalid; } else if (status.HasFlag(ValidationStatus.PasswordShort)) { errors[key] = Constants.PasswordShort; } else if (status.HasFlag(ValidationStatus.PhoneInvalid)) { errors[key] = Constants.PhoneInvalid; } else if (status.HasFlag(ValidationStatus.EmailInvalid)) { errors[key] = Constants.EmailInvalid; } else if (status.HasFlag(ValidationStatus.VkLinkInvalid)) { errors[key] = Constants.VkLinkInvalid; } else if (status.HasFlag(ValidationStatus.CategoryInvalid)) { errors[key] = Constants.CategoryEmpty; } }