title: .Net XML Serialization issue
body: <p>My confusion is, I am using .Net C# XMLSerializer to serialize a customize defined type, using the schema/cs file generated by XSD tool from an input original XML file. But the generated serialized XML file namespace is different from original XML input file. Especially from original XML file, Envelope belongs to namespace soapenv, but in the serialized XML file, Envelope belongs to default namespace. Any ideas what is wrong?</p>
  
  <p>Here is how I use XSD tool to generate schema file and cs file for the customized defined type from XML input file XMLFile1.xml,</p>
  
  <p>BTW: XML files can be downloaded from, </p>
  
  <p><a href="http://www.mediafire.com/?nwngwzz3gmm" rel="nofollow noreferrer">http://www.mediafire.com/?nwngwzz3gmm</a></p>
  
  <pre><code>D:\&gt;xsd XMLFile1.xml
  Microsoft (R) Xml Schemas/DataTypes support utility
  [Microsoft (R) .NET Framework, Version 2.0.50727.3038]
  Copyright (C) Microsoft Corporation. All rights reserved.
  Writing file 'D:\XMLFile1.xsd'.
  
  D:\&gt;xsd XMLFile1.xsd XMLFile1_app1.xsd /classes
  Microsoft (R) Xml Schemas/DataTypes support utility
  [Microsoft (R) .NET Framework, Version 2.0.50727.3038]
  Copyright (C) Microsoft Corporation. All rights reserved.
  Writing file 'D:\XMLFile1_XMLFile1_app1.cs'.
  </code></pre>
  
  <p>Here is how I use C# code to serialize the file and output to TestOutputFile.xml,</p>
  
  <pre><code>static void Main(string[] args)
  {
      Envelope en = new Envelope();
      en.Items = new EnvelopeBody[1];
      en.Items[0] = new EnvelopeBody();
      en.Items[0].QueryResponse = new QueryResponseFaculties[1];
      en.Items[0].QueryResponse[0] = new QueryResponseFaculties();
      en.Items[0].QueryResponse[0].Name = "John";
  
      XmlSerializer serializer =
       new XmlSerializer(typeof(Envelope));
      TextWriter writer = new StreamWriter("TestOutputFile.xml");
  
      serializer.Serialize(writer, en);
      writer.Close();
  
      return;
  }
  </code></pre>
  
  <p>The original XML file is,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soapenv:Body&gt;
      &lt;QueryResponse xmlns="http://schemas.mycorp.com/test"&gt;
        &lt;Faculties&gt;
          &lt;Name&gt;
            John
          &lt;/Name&gt;
        &lt;/Faculties&gt;
      &lt;/QueryResponse&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>The serialized XML file is,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;Body&gt;
      &lt;QueryResponse xmlns="http://schemas.mycorp.com/test"&gt;
        &lt;Faculties&gt;
          &lt;Name&gt;John&lt;/Name&gt;
        &lt;/Faculties&gt;
      &lt;/QueryResponse&gt;
    &lt;/Body&gt;
  &lt;/Envelope&gt;
  </code></pre>
  
  <p>EDIT 1:</p>
  
  <p>Current code is,</p>
  
  <pre><code>static void Main(string[] args)
  {
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
      ns.Add("", "http://schemas.mycorp.com/test");
      ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
      ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  
      Envelope en = new Envelope();
      en.Items = new EnvelopeBody[1];
      en.Items[0] = new EnvelopeBody();
      en.Items[0].QueryResponse = new QueryResponseFaculties[1];
      en.Items[0].QueryResponse[0] = new QueryResponseFaculties();
      en.Items[0].QueryResponse[0].Name = "John";
  
      XmlSerializer serializer =
       new XmlSerializer(typeof(Envelope));
      TextWriter writer = new StreamWriter("TestOutputFile.xml");
  
      serializer.Serialize(writer, en, ns);
      writer.Close();
  
      return;
  }
  </code></pre>
  
  <p>Current output (serialized XML file) is,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://schemas.mycorp.com/test"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soapenv:Body&gt;
      &lt;QueryResponse&gt;
        &lt;Faculties&gt;
          &lt;Name&gt;John&lt;/Name&gt;
        &lt;/Faculties&gt;
      &lt;/QueryResponse&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I want the output to be (notice the location of string xmlns="http://schemas.mycorp.com/test"),</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soapenv:Body&gt;
      &lt;QueryResponse xmlns="http://schemas.mycorp.com/test"&gt;
        &lt;Faculties&gt;
          &lt;Name&gt;John&lt;/Name&gt;
        &lt;/Faculties&gt;
      &lt;/QueryResponse&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>thanks in advance,
  George</p>