【问题标题】:Control validation against XmlSchemaSimpleTypeRestriction.Facets针对 XmlSchemaSimpleTypeRestriction.Facets 控制验证
【发布时间】:2014-03-04 10:01:32
【问题描述】:

我有一个带有 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 事件中使用此方法。有没有更简单的方法来做到这一点?

【问题讨论】:

    标签: c# xml validation xsd


    【解决方案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"));
        }
    

    【讨论】:

    • XmlSchema 不包含任何验证方法。
    • 不客气 :)。只是出于好奇:你从哪里得到XmlSchemaSimpleTypeRestriction
    • 我通过将 XmlShemaSimpleType.Content 转换为 XmlSchemaSimpleTypeRestriction 来得到它
    猜你喜欢
    • 2012-02-07
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多