title: Can I have null attribute and other attribute at the same tag in XML created by XSD C# generated class?
body: <p>I have a bunch of C# classes, which are auto generated from an XSD. Then I generate XML files based on those C# classes. Nothing existing so far. </p>
  
  <p><strong>The problem:</strong></p>
  
  <p>The generated XML files are going through validation and the validation requires an extra attribute to all XML tags with <code>xsi:nil="true"</code>. Basically the tags should look like :  <code>&lt;testTag.01 xsi:nil="true" NV="123123" /&gt;</code>, but I can't achieve that in C#.  My code is:</p>
  
  <blockquote>
  <pre><code>     if (myObject.TestTag.HasValue)
          {
              t.testTag01 = new testTag01();
              t.testTag01.Value = myObject.TestTag.Value;
          }
          //else
          //{
          //    t.testTag01 = new testTag01();
          //    t.testTag01.NV = "123123";//Not Recorded
          //}
  </code></pre>
  </blockquote>
  
  <p>This code generates <code>&lt;testTag.01&gt;SomeValue&lt;/testTag.01&gt;</code> or <code>&lt;testTag.01 xsi:nil="true"/&gt;</code>. </p>
  
  <p>If I uncomment the <strong>ELSE</strong>, the result would be: <code>&lt;testTag.01&gt;SomeValue&lt;/testTag.01&gt;</code> or <code>&lt;testTag.01 NV="123123" /&gt;</code>. </p>
  
  <p>So I have no idea how to get to the format, which is required by the validation tool. Any ideas ?</p>
  
  <p>P.S.</p>
  
  <p>Here is the auto-generated C# class:</p>
  
  <blockquote>
    <p>///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
    "4.0.30319.33440")] [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,
    Namespace="http://www.blabla.org")]</p>
    
    <p>public partial class testTag01 {</p>
  
  <pre><code>private string nvField;
  
  private SomeEnum valueField;
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlAttributeAttribute()]
  public string NV {
      get {
          return this.nvField;
      }
      set {
          this.nvField = value;
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTextAttribute()]
  public SomeEnum Value {
      get {
          return this.valueField;
      }
      set {
          this.valueField = value;
      }
  } }
  </code></pre>
  </blockquote>
  
  <p>I wouldn't like to alter that part, but I understand it is impossible without doing it. Also I have tried to set SomeEnum to be Nullable. <code>public SomeEnum? Value</code>, but is throwing an exception:</p>
  
  <pre><code>Cannot serialize member 'Value' of type System.Nullable`1[]. XmlAttribute/XmlText cannot be used to encode complex types.
  </code></pre>