title: C# XML Serializer Nested Elements Access Objects
body: <p>I have an XSD generated by the XSD.exe tool. I am loading data from an Excel sheet and filling a data table, then I want to save that data to an XML file. How do I serialize the nested elements?</p>
  
  <p>in my schema: (under Providers there can be multiple Provider and under ProviderSites there can be multiple ProviderSite)</p>
  
  <pre><code>&lt;xs:element name="Providers"&gt;
   &lt;xs:complexType&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element maxOccurs="unbounded" name="Provider"&gt;
        &lt;xs:complexType&gt;
          &lt;xs:all&gt;
            &lt;xs:element minOccurs="0" name="FederalTaxIdentifier" type="xs:string" /&gt;
            &lt;xs:element minOccurs="0" name="ProviderName" type="xs:string" /&gt;
            &lt;xs:element minOccurs="0" name="ContractualRelationshipCode" type="xs:string" /&gt;
            &lt;xs:element minOccurs="0" name="ProviderSites"&gt;
              &lt;xs:complexType&gt;
                &lt;xs:sequence&gt;
                  &lt;xs:element maxOccurs="unbounded" name="ProviderSite"&gt;
                    &lt;xs:complexType&gt;
                      &lt;xs:all&gt;
                        &lt;xs:element minOccurs="0" name="SiteIdentifier" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="SiteName" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="LicenseTypeCode" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="OpenDate" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="CloseDate" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="DirectorPrefixName" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="DirectorFirstName" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="DirectorMiddleInitial" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="DirectorLastName" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="DirectorPhoneNumber" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="NationalProviderIdentifier" type="xs:string" /&gt;
                        &lt;xs:element minOccurs="0" name="WebAddress" type="xs:string" /&gt;
                      &lt;/xs:all&gt;
                    &lt;/xs:complexType&gt;
                  &lt;/xs: element&gt;
              &lt;/xs:complexType&gt;
          &lt;/xs:all&gt;   
      &lt;/xs:element&gt;       
    &lt;/xs:sequence&gt;
  </code></pre>
  
  <p>
   </p>
  
  <p>in my c# I created a new instance of the element Providers->Provider</p>
  
  <pre><code>ProvidersProvider prov = new ProvidersProvider();
  </code></pre>
  
  <p>and I access an object and set it to the cell in my data table</p>
  
  <pre><code>prov.FederalTaxIdentifier = DtRow.ItemArray[0].ToString();
  </code></pre>
  
  <p>then serialize it to the xml</p>
  
  <pre><code>serializer.Serialize(writer, prov);
  </code></pre>
  
  <p>Trying to figure out how to access and add the other elements like Providers->Provider->ProviderSites->ProviderSite->SiteIdentifier</p>
  
  <p><strong>UPDATE</strong>
  I added a xs:choice element to the xsd and re-generated the class. 
  How do I go about setting those elements?</p>
  
  <pre><code>&lt;xs:element minOccurs="0" name="ProviderSitePhones"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element maxOccurs="unbounded" name="ProviderSitePhone"&gt;
                  &lt;xs:complexType&gt;
                      &lt;xs:sequence&gt;
                         &lt;xs:choice maxOccurs="unbounded"&gt;
                            &lt;xs:element minOccurs="0" name="TypeCode" type="xs:string" /&gt;
                             &lt;xs:element minOccurs="0" name="PhoneNumber" type="xs:string" /&gt;
                          &lt;/xs:choice&gt;
                      &lt;/xs:sequence&gt;
                      &lt;xs:attribute name="action" type="xs:string" use="optional" /&gt;
                  &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
         &lt;/xs:sequence&gt;
     &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p>In the XSD class:</p>
  
  <pre><code>/// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
  public partial class ProvidersProviderProviderSiteProviderSitePhone {
  
      private string[] itemsField;
  
      private ItemsChoiceType[] itemsElementNameField;
  
      private string actionField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute("PhoneNumber", typeof(string))]
      [System.Xml.Serialization.XmlElementAttribute("TypeCode", typeof(string))]
      [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
      public string[] Items {
          get {
              return this.itemsField;
          }
          set {
              this.itemsField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
      [System.Xml.Serialization.XmlIgnoreAttribute()]
      public ItemsChoiceType[] ItemsElementName {
          get {
              return this.itemsElementNameField;
          }
          set {
              this.itemsElementNameField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string action {
          get {
              return this.actionField;
          }
          set {
              this.actionField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
  [System.SerializableAttribute()]
  [System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
  public enum ItemsChoiceType {
  
      /// &lt;remarks/&gt;
      PhoneNumber,
  
      /// &lt;remarks/&gt;
      TypeCode,
  }
  </code></pre>
  
  <p>In my Program.cs I have:</p>
  
  <pre><code>ProviderSitePhones = provider.Value.Select(ProviderSitePhone =&gt; new ProvidersProviderProviderSiteProviderSitePhone()
  {
  
  }).ToArray()
  </code></pre>