title: problems building xml document programmatically with xsd.exe generated code when containing <xs:any>
body: <p>As the title states, i'm trying to programmatically build an xml document. i have written and compiled the schemas as correctly as i know how, but the &lt;xs:any&gt; implementation is tripping me up when trying to fill in elements defined in those sections.</p>
  <p>Below are 2 example schemas that i am using. The Primary.xsd optionally uses an element defined in Dependency.xsd. They are compiled with xsd.exe using .net 4.8.</p>
  <p>Primary.xsd</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;xs:schema id=&quot;Primary&quot;
      targetNamespace=&quot;http://tempuri.org/Primary.xsd&quot;    
      elementFormDefault=&quot;qualified&quot;
      xmlns=&quot;http://tempuri.org/Primary.xsd&quot;
      xmlns:mstns=&quot;http://tempuri.org/Primary.xsd&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
      xmlns:dns=&quot;http://tempuri.org/Dependency.xsd&quot;&gt;
    
    &lt;xs:import namespace=&quot;http://tempuri.org/Dependency.xsd&quot; schemaLocation=&quot;Dependency.xsd&quot;/&gt;
  
    &lt;xs:element name=&quot;user&quot;&gt;
      &lt;xs:complexType&gt;
        &lt;xs:attribute name=&quot;firstName&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/&gt;
        &lt;xs:attribute name=&quot;lastName&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
      
    &lt;xs:element name=&quot;root&quot;&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element ref=&quot;user&quot; minOccurs=&quot;1&quot;/&gt;
          &lt;xs:any processContents=&quot;lax&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; namespace=&quot;http://tempuri.org/Dependency.xsd&quot; /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;    
    &lt;/xs:element&gt;  
  &lt;/xs:schema&gt;
  </code></pre>
  <p>Dependency.xsd</p>
  <pre><code>   &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;xs:schema id=&quot;Dependency&quot;
                 targetNamespace=&quot;http://tempuri.org/Dependency.xsd&quot;
                 elementFormDefault=&quot;qualified&quot;
                 xmlns=&quot;http://tempuri.org/Dependency.xsd&quot;
                 xmlns:mstns=&quot;http://tempuri.org/Dependency.xsd&quot;
                 xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
      
        &lt;xs:simpleType name=&quot;systemStateOption&quot;&gt;
          &lt;xs:restriction base=&quot;xs:string&quot;&gt;
            &lt;xs:enumeration value=&quot;off&quot;/&gt;
            &lt;xs:enumeration value=&quot;running&quot;/&gt;
            &lt;xs:enumeration value=&quot;error&quot;/&gt;
          &lt;/xs:restriction&gt;
        &lt;/xs:simpleType&gt;
      
        &lt;xs:element name=&quot;flexibleDetail&quot;&gt;
          &lt;xs:complexType&gt;
            &lt;xs:attribute name=&quot;systemState&quot; type=&quot;mstns:systemStateOption&quot; use=&quot;optional&quot;/&gt;
            &lt;xs:anyAttribute processContents=&quot;lax&quot;/&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
        
      &lt;/xs:schema&gt;
  </code></pre>
  <p>Compiled code:</p>
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     Runtime Version:4.0.30319.42000
  //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  using System.Xml.Serialization;
  
  // 
  // This source code was auto-generated by xsd, Version=4.8.3928.0.
  // 
  
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;xsd&quot;, &quot;4.8.3928.0&quot;)]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;)]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=&quot;http://tempuri.org/Dependency.xsd&quot;)]
  [System.Xml.Serialization.XmlRootAttribute(Namespace=&quot;http://tempuri.org/Dependency.xsd&quot;, IsNullable=false)]
  public partial class flexibleDetail : object, System.ComponentModel.INotifyPropertyChanged {
      
      private systemStateOption systemStateField;
      
      private bool systemStateFieldSpecified;
      
      private System.Xml.XmlAttribute[] anyAttrField;
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public systemStateOption systemState {
          get {
              return this.systemStateField;
          }
          set {
              this.systemStateField = value;
              this.RaisePropertyChanged(&quot;systemState&quot;);
          }
      }
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlIgnoreAttribute()]
      public bool systemStateSpecified {
          get {
              return this.systemStateFieldSpecified;
          }
          set {
              this.systemStateFieldSpecified = value;
              this.RaisePropertyChanged(&quot;systemStateSpecified&quot;);
          }
      }
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAnyAttributeAttribute()]
      public System.Xml.XmlAttribute[] AnyAttr {
          get {
              return this.anyAttrField;
          }
          set {
              this.anyAttrField = value;
              this.RaisePropertyChanged(&quot;AnyAttr&quot;);
          }
      }
      
      public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
      
      protected void RaisePropertyChanged(string propertyName) {
          System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
          if ((propertyChanged != null)) {
              propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;xsd&quot;, &quot;4.8.3928.0&quot;)]
  [System.SerializableAttribute()]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace=&quot;http://tempuri.org/Dependency.xsd&quot;)]
  public enum systemStateOption {
      
      /// &lt;remarks/&gt;
      off,
      
      /// &lt;remarks/&gt;
      running,
      
      /// &lt;remarks/&gt;
      error,
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;xsd&quot;, &quot;4.8.3928.0&quot;)]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;)]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=&quot;http://tempuri.org/Primary.xsd&quot;)]
  [System.Xml.Serialization.XmlRootAttribute(Namespace=&quot;http://tempuri.org/Primary.xsd&quot;, IsNullable=false)]
  public partial class user : object, System.ComponentModel.INotifyPropertyChanged {
      
      private string firstNameField;
      
      private string lastNameField;
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string firstName {
          get {
              return this.firstNameField;
          }
          set {
              this.firstNameField = value;
              this.RaisePropertyChanged(&quot;firstName&quot;);
          }
      }
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string lastName {
          get {
              return this.lastNameField;
          }
          set {
              this.lastNameField = value;
              this.RaisePropertyChanged(&quot;lastName&quot;);
          }
      }
      
      public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
      
      protected void RaisePropertyChanged(string propertyName) {
          System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
          if ((propertyChanged != null)) {
              propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;xsd&quot;, &quot;4.8.3928.0&quot;)]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;)]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=&quot;http://tempuri.org/Primary.xsd&quot;)]
  [System.Xml.Serialization.XmlRootAttribute(Namespace=&quot;http://tempuri.org/Primary.xsd&quot;, IsNullable=false)]
  public partial class root : object, System.ComponentModel.INotifyPropertyChanged {
      
      private user userField;
      
      private System.Xml.XmlElement[] anyField;
      
      /// &lt;remarks/&gt;
      public user user {
          get {
              return this.userField;
          }
          set {
              this.userField = value;
              this.RaisePropertyChanged(&quot;user&quot;);
          }
      }
      
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAnyElementAttribute()]
      public System.Xml.XmlElement[] Any {
          get {
              return this.anyField;
          }
          set {
              this.anyField = value;
              this.RaisePropertyChanged(&quot;Any&quot;);
          }
      }
      
      public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
      
      protected void RaisePropertyChanged(string propertyName) {
          System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
          if ((propertyChanged != null)) {
              propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  <p>Test code to build message:</p>
  <pre><code>static void Main(string[] args)
  {
      root docRoot = new root();
  
      user newUser = new user();
      newUser.firstName = &quot;Herman&quot;;
      newUser.lastName = &quot;McMullen&quot;;
  
      docRoot.user = newUser;
  
      //building the optional element here!
      flexibleDetail detail = new flexibleDetail();
      detail.systemState = systemStateOption.running;
      detail.systemStateSpecified = true;
  
      // This is the best i can do...?
      // I don't like having to rely on raw strings. that is what
      // the code generator should build for me.
      XmlDocument doc = new XmlDocument();
  
      var element = doc.CreateElement(detail.ToString());
      element.SetAttribute(&quot;systemState&quot;, detail.systemState.ToString());
      docRoot.Any[0] = element;
      string output = &quot;&quot;;
      XmlSerializer serializer = new XmlSerializer(docRoot.GetType());
  
      // **** Even if i was able to populate, this threw an exception
      using (StringWriter textWriter = new StringWriter())
      {
          serializer.Serialize(textWriter, docRoot);
          output = textWriter.ToString();
      }
  }
  </code></pre>