2014-03-04 3 views
0

일부 TextBox 컨트롤이 포함 된 System.Windows.Form이있는 데스크톱 응용 프로그램이 있습니다. xml 스키마의 제한에 대한 컨트롤 값의 유효성을 검사해야합니다. 내가 컨트롤의 유효성 검사 이벤트에서이 방법을 사용하기 위하여려고하고있다XmlSchemaSimpleTypeRestriction.Facets에 대한 컨트롤 유효성 검사

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; 
    } 

: 나는 그것의 유형에서 관련 XmlSchemaSimpleTypeRestriction를 검색하고 그 값을 검증하기 위해 다음과 같이 방법을 사용할 수있는 각 텍스트 상자에 대한

. 이 작업을 수행하는 간단한 방법이 있습니까?

답변

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

관련 문제