title: XML Schema How to Restrict element by minLength \ maxLength
body: <p>I've been trying to solve this issue for a few hours now, the problem is that my validation process doesn't really take into account anything I type inside the restriction.</p>
  
  <p>This is my XSD:</p>
  
  <pre><code>&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tempuri.org/myxmltest"&gt;
      &lt;xs:element name="Students_With_Preferences-xml"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="Student-xml" maxOccurs="unbounded" minOccurs="0"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element type="xs:string" name="District"/&gt;
                              &lt;xs:element name="Preferences"&gt;
                                  &lt;xs:simpleType&gt;
                                      &lt;xs:restriction base="xs:string"&gt;
                                          &lt;xs:minLength value="5"/&gt;
                                          &lt;xs:maxLength value="5"/&gt;
                                      &lt;/xs:restriction&gt;
                                  &lt;/xs:simpleType&gt;
                              &lt;/xs:element&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Xml Sample:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Students_With_Preferences-xml xmlns="http://tempuri.org/myxmltest"&gt;                                 
      &lt;Student-xml&gt;
          &lt;District&gt;b&lt;/District&gt;                              
          &lt;Preferences&gt;(sc1,20)|(sc2,15)&lt;/Preferences&gt;        
      &lt;/Student-xml&gt;
      &lt;Student-xml&gt;
          &lt;District&gt;d&lt;/District&gt;
          &lt;Preferences&gt;(sc2,15)|(sc1,10)&lt;/Preferences&gt;    
      &lt;/Student-xml&gt;
  &lt;/Students_With_Preferences-xml&gt;
  </code></pre>
  
  <p>As you can see only element with length 5 is allowed, but when I run my code it allows any length.
  Just to be clear, everything else is validate properly, just this annoying error.</p>
  
  <p>I tried to fix this issue using the regex option but as u guessed its not working as well.</p>
  
  <p>Validation Code(C#):</p>
  
  <pre><code>    private Boolean ValidateXML(HttpPostedFileBase file, string xsd_path)
      {
          bool errors = false;
          string filename = "";
          XmlSchemaSet schemas = new XmlSchemaSet();
          XmlDocument xml_doc = new XmlDocument();
          try
          {
              filename = file.FileName;
              schemas.Add("", xsd_path);
              xml_doc.Load(file.InputStream);
              foreach (XmlNode node in xml_doc.ChildNodes.Item(1).ChildNodes)
              {
                  XDocument doc = XDocument.Parse(node.OuterXml);
                  doc.Validate(schemas, (o, e) =&gt;
                  {
                      ViewBag.msg = "Error validating - " + filename + " - " + e.Message;
                      errors = true;
                  });
                  if (errors) break;
              }
          }
          catch (Exception ex)
          {
              errors = true;
              string msg = "Error validating - " + filename + " - " + ex.Message;
              throw new Exception(msg);
          }
          if (!errors)
          {
              ViewBag.msg = filename + " was validated succesfully";
          }
  
          return !errors;
      }
  </code></pre>
  
  <p>I would really appreciate any help on how to fix this issue, Thanks!</p>