针对XmlSchemaSimpleTypeRestriction.Facets的控制验证

时间:2014-03-04 10:01:33

标签: c# xml validation xsd

我有一个带有System.Windows.Form的桌面应用程序,其中包含一些TextBox控件。我需要根据xml架构的限制来验证控件值。

对于每个TextBox,我可以从其类型中检索相关的XmlSchemaSimpleTypeRestriction,然后使用如下方法验证其值:

 public static bool Validate(XmlSchemaSimpleTypeRestriction restriction, string value)
    {
        bool isENum = false;
        bool isValidEnum = false;
        foreach (var item in restriction.Facets)
        {
            XmlSchemaLengthFacet lengthFacet = item as XmlSchemaLengthFacet;
            if (lengthFacet != null)
            {
                int length = Int32.Parse(lengthFacet.Value);
                if (!(value.Length == length))
                    return false;
            }

            XmlSchemaMinLengthFacet minLenghtFacet = item as XmlSchemaMinLengthFacet;
            if (minLenghtFacet != null)
            {
                int length = Int32.Parse(minLenghtFacet.Value);
                if (!(value.Length >= length))
                    return false;
            }

            XmlSchemaMaxLengthFacet maxLenghtFacet = item as XmlSchemaMaxLengthFacet;
            if (maxLenghtFacet != null)
            {
                int length = Int32.Parse(maxLenghtFacet.Value);
                if (!(value.Length <= length))
                    return false;
            }

            XmlSchemaPatternFacet patternFacet = item as XmlSchemaPatternFacet;
            if (patternFacet != null)
            {
                Regex re = new Regex(patternFacet.Value);
                if (!re.IsMatch(value))
                    return false;
            }

            XmlSchemaEnumerationFacet enumFacet = item as XmlSchemaEnumerationFacet;
            if (patternFacet != null)
            {
                isENum = true;
                if (StringComparer.InvariantCultureIgnoreCase.Compare(value, enumFacet.Value) == 0)
                    isValidEnum = true;
            }
            if (isENum && (!isValidEnum))
                return false;


        return true;
    }

我将在控件的Validating事件中使用此方法。有没有更简单的方法呢?

1 个答案:

答案 0 :(得分:1)

好吧,它比我最初想的要复杂一点。基本上,您需要创建一个XmlSchema,期望具有所提供限制的单个元素。然后使用提供的值创建XML元素,并使用XmlReader

对模式进行验证
    public static bool Validate(XmlSchemaSimpleTypeRestriction restriction, string value)
    {
        var schema = new XmlSchema();
        schema.Items.Add(new XmlSchemaElement
        {
            Name = "value",
            SchemaType = new XmlSchemaSimpleType { Content = restriction }
        });

        var schemaSet = new XmlSchemaSet();
        schemaSet.Add(schema);

        var readerSettings = new XmlReaderSettings
        {
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
            Schemas = schemaSet
        };

        string xml = new XElement("value", value).ToString();

        try
        {
            var reader = XmlReader.Create(new StringReader(xml), readerSettings);
            while (reader.Read()) ;
            return true;
        }
        catch (XmlSchemaValidationException)
        {
            return false;
        }
    }

我用这段代码测试了它:

    static void Main(string[] args)
    {
        var restriction = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") };
        restriction.Facets.Add(new XmlSchemaMinLengthFacet { Value = "3" });
        Console.WriteLine(Validate(restriction, "str"));
    }