title: Adding restrictions to xsd generated from wcf
body: <p>So I'm doing some research on how to format the WSDL and XSD generated from WCF, specifically adding annotations/documentation to the WSDL and XSDs along with adding restrictions to various parameters.</p>
  
  <p>So far I've been able to add the documentation to both the WSDL and XSDs by creating attributes that implement the IWSDLExportExtension interface along with the IOperationBehavior Interface. </p>
  
  <p>For the general idea of modifying the schemas see:<br>
  <a href="http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx" rel="nofollow">http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx</a></p>
  
  <p>For the general idea of adding annotations to WSDL see:<br>
  <a href="http://msdn.microsoft.com/en-us/library/ms731731(v=vs.110).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms731731(v=vs.110).aspx</a></p>
  
  <p>However I run into trouble when trying to add restrictions to elements(by adding simple types) in the xsd.</p>
  
  <p>From here I can either get an exception to occur stating that I can't set the elements type because it already has a read-only type associated with it, or I can try using the read-only type to add the restriction, but then nothing happens.</p>
  
  <p>Here's the code for generating the exception 
  <code>(System.Xml.Schema.XmlSchemaException: The type attribute cannot be present with either simpleType or complexType.)</code>
  : </p>
  
  <pre><code>    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
      var Sequence = ComplexType.Particle as XmlSchemaSequence;
  
      foreach (XmlSchemaElement Item in Sequence.Items)
      {
          if (Item.Name = Parameter.Name &amp;&amp; Parameter.Length &gt; 0)
          {
              XmlSchemaSimpleType tempType = new XmlSchemaSimpleType();
              XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
              XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
              tempType.Content = tempRestriction;
              tempRestriction.Facets.Add(lengthFacet);
              lengthFacet.Value = "" + Parameter.Length;
  
              Item.SchemaType = tempType; // &lt;-- Problem code
  
           }
           ...
       }
  </code></pre>
  
  <p>And here's the work around that does nothing:</p>
  
  <pre><code>    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
      var Sequence = ComplexType.Particle as XmlSchemaSequence;
  
      foreach (XmlSchemaElement Item in Sequence.Items)
      {
          if (Item.Name = Parameter.Name &amp;&amp; Parameter.Length &gt; 0)
          {
              XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
              XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
              tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
              tempRestriction.Facets.Add(lengthFacet);
              lengthFacet.Value = "" + Parameter.Length;
  
              // Appears to do nothing
              ((XmlSchemaSimpleType)Item.ElementSchemaType).Content = tempRestriction;
  
           }
           ...
       }
  </code></pre>
  
  <p>Quick other note:
     If I switch to a normal for loop and actually try to replace the trouble element with a new element (hackish I know...), I get the following exception: <code>System.InvalidCastException: Unable to cast object of type 'System.Xml.Schema.XmlSchemaSimpleType' to type 'System.Xml.Schema.XmlSchemaParticle'.</code>
  This one I'm more just curious about since they both should be XmlSchemaElements right?</p>
  
  <p>So basically, does anyone know how to add simple types/add simple type restrictions to xmlschemaelements and have it show up in the XSDs that are generated with the WSDL from WCF?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>EDIT:
  Added 
  <br><code>tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");</code></p>