2014-03-04 78 views
0

我有一個包含一些TextBox控件的System.Windows.Form的桌面應用程序。我需要驗證控制值與xml模式的限制。控制驗證針對XmlSchemaSimpleTypeRestriction.Facets

對於每一個文本框,我可以檢索其類型的相關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; 
    } 

我會在控件的驗證事件來使用此方法。有沒有更簡單的方法來做到這一點?

回答

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")); 
    } 
+0

XMLSchema的不包含任何驗證方法。 – Trifon

+0

啊,你說得對。編輯答案。 – alsed42

+0

謝謝! ...它工作正常。 – Trifon