首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >针对XmlSchemaSimpleTypeRestriction.Facets的控件验证

针对XmlSchemaSimpleTypeRestriction.Facets的控件验证
EN

Stack Overflow用户
提问于 2014-03-04 10:01:32
回答 1查看 358关注 0票数 1

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

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

代码语言:javascript
复制
 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;
    }

我将在控件的验证事件中使用此方法。有什么更简单的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 10:10:37

好吧,这比我最初想的要复杂一些。基本上,您需要创建一个XmlSchema,它需要具有所提供的限制的单个元素。然后,使用提供的值创建XML元素,并使用XmlReader对其进行模式验证。

代码语言:javascript
复制
    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;
        }
    }

我用以下代码测试了它:

代码语言:javascript
复制
    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"));
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22168629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档