title: Change autogenerated class' XmlSerialization's namespace handling
body: <p>I'm currently in the process to create a bunch of files that should then be used by a different programm. Most of them are XML-Files. Naturally, I extracted the .xsd files from the program and used the <a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe" rel="nofollow noreferrer">xsd.exe</a> tool to autogenerate C#-classes, which works rather well.</p>
  
  <h1>The problem</h1>
  
  <p>Serializing the autogenerated class results in a xml file like this:</p>
  
  <pre><code>&lt;root xmlns="ns1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;group xmlns=""&gt;
          &lt;item&gt;foo&lt;/item&gt;
          &lt;item&gt;bar&lt;/item&gt;
      &lt;/group&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Both <code>xsi</code> and <code>xsd</code> aren't used in anything that I have found so far, but that shouldn't really be a problem.</p>
  
  <p>The XML that the program would expect would look something like this:</p>
  
  <pre><code>&lt;n1:root xmlns:n1="ns1"&gt;
      &lt;group&gt;
          &lt;item&gt;foo&lt;/item&gt;
          &lt;item&gt;bar&lt;/item&gt;
      &lt;/group&gt;
  &lt;/n1:root
  </code></pre>
  
  <p>Both xml should result in the same thing when deserialized, so I do not put the fault onto xsd.exe.</p>
  
  <p>However, when trying to open the generated xml in the program, it produces an "Object reference not set to an instance of an object" error. Both the <code>xmlns:xsi</code> and <code>xmlns:xsd</code> have to be removed and it has to use <code>xmlns:n1</code> instead of the default namespace.</p>
  
  <h1>What I have tried</h1>
  
  <p>At first I thought, I could just manually serialize the class using <code>IXmlSerializable</code>, however that produces a runtime error when serializing, since xsd.exe automatically adds <code>XmlTypeAttribute</code> and <code>XmlRootAttribute</code>.</p>
  
  <p>The produced error reads <code>InvalidOperationException: Only XmlRoot attribute may be specified for the type myNs.MyClass. Please use XmlSchemaProviderAttribute to specify schema type.</code></p>
  
  <p>I don't think using XmlSchemaProviderAttribute is a good idea, because that defeats the idea of autogenerating the class from the given schema. (The schema is probably gonna change in future versions of the program)</p>
  
  <hr>
  
  <p>If you want a minimal example, here is some code that runs on <a href="http://rextester.com" rel="nofollow noreferrer">rextester.com</a>: (note that rextester uses .NET framework 4.5, while I am using .NET framework 4.7, so any answers using new features are more than welcome)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text.RegularExpressions;
  using System.Xml;
  using System.Xml.Schema;
  using System.Xml.Serialization;
  
  namespace Rextester
  {
      // AUTOGENERATED
      [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2558.0")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.somens.com")]
      [System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.somens.com", IsNullable=false)]
      public partial class Test {
          [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
          [System.Xml.Serialization.XmlArrayItemAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
          public Item[] Group { get; set; }
      }
  
      [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2558.0")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.somens.com")]
      public partial class Item {
          [System.Xml.Serialization.XmlAttributeAttribute()]
          public int Value { get; set; }
      }
  
      // CUSTOM
      public partial class Test : IXmlSerializable
      {
          public XmlSchema GetSchema()
          {
              return null;
          }
  
          public void ReadXml(XmlReader reader)
          {
              throw new NotImplementedException();
          }
  
          public void WriteXml(XmlWriter writer)
          {
              foreach (var item in Group) {
                  writer.WriteStartElement("item");
                  writer.WriteAttributeString("Value", item.Value.ToString());
                  writer.WriteEndElement();
              }
          }
  
      }
  
  
      public class Program
      {
          public static void Main(string[] args)
          {
              var t = new Test();
              t.Group = new Item[] { new Item { Value = 5}, new Item { Value = 10} };
  
              var serializer = new XmlSerializer(typeof(Test));
              serializer.Serialize(Console.Out, t);
          }
      }
  }
  </code></pre>