title: Serializing object to string XML with multiple namespaces
body: <p>I'm trying to serialize an object into a string.</p>
  
  <p>The xml from which the c# models was taken had multiple namespaces:</p>
  
  <pre><code>xmlns="http://www.example.org/standards/def/1" 
  xmlns:ac="http://www.example.org/Standards/xyz/1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:rlc="http://www.example.org/standards/def/1" 
  xmlns:def1="http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd"
  </code></pre>
  
  <p>I'm serializing it with:</p>
  
  <pre><code>var deserialize = (MyType)pageDeserializer.Deserialize(reader);
  var namespaces = new XmlSerializerNamespaces();
  namespaces.Add("ac", "urn:http://www.example.org/Standards/xyz/1");
  namespaces.Add("rlc", "urn:http://www.example.org/standards/def/1");
  namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd");
  
  
  var str = pageDeserializer.SerializeAsUtf8&lt;JvInsReinsurance&gt;(deserialize, namespaces);
  </code></pre>
  
  <p>Where the method SerializeAsUtf8 is:</p>
  
  <pre><code>public static string SerializeAsUtf8&lt;T&gt;(this XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
  {
      using (var textWriter = new Utf8StringWriter())
      {
          serializer.Serialize(textWriter, o, ns);
          return textWriter.ToString();
      }
  }
  </code></pre>
  
  <p>I was expecting my XML to look like:</p>
  
  <pre><code>   &lt;rlc:element1 attribute1="value"&gt;
     &lt;ac:element1&gt;VALUR&lt;/ac:element1&gt;
     &lt;/rlc:element1&gt;
  </code></pre>
  
  <p>What I get is:</p>
  
  <pre><code>   &lt;element1 attribute1="value"&gt;
     &lt;element1&gt;VALUR&lt;/element1&gt;
     &lt;/element1&gt;
  </code></pre>
  
  <p>But the information for the namespace is not included, and this makes the subsequent xsd validation fail. How can I get the namespace prefixes included?</p>
  
  <p>UPDATE 1</p>
  
  <p>Removing the urn as suggested in the comments, made me go past the first step. Now I'm getting an error when validating against the XSD.</p>
  
  <p>I get the following errors:</p>
  
  <p>1.</p>
  
  <pre><code>The element 'ElementX' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementY' in namespace 'http://www.example.org/standards/def/1'.
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>The element 'ElementP' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementQ' in namespace 'http://www.example.org/standards/def/1'.
  </code></pre>
  
  <p>For 1. the classes are</p>
  
  <pre><code>[System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public partial class ElementX
  {
      [XmlElement("ElementYName")]
      public ElementY[] ElementYNames { get; set; }
  }
  
  
  [System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public partial class ElementY
  {
  
      [XmlAttribute]
      public string Field1 { get; set; }
  
      public ElementYFieldAmountType FieldAmount { get; set; }
  
      public string Field2 { get; set; }
  
  
      private string field3;
  
  
  /// &lt;remarks/&gt;
  public string Field3
  {
      get
      {
          return this.field3;
      }
      set
      {
          this.field3 = value;
      }
  }
  
  }
  
  [Serializable]
  [DesignerCategory("code")]
  [XmlType(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public class ElementYFieldAmountType
  {
      public FieldAmount Amt { get; set; }
  }
  
      [System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public class FieldAmount
  {
  
      private string _ccyField;
  
      private decimal valueField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Ccy
      {
          get
          {
              return this._ccyField;
          }
          set
          {
              this._ccyField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlTextAttribute()]
      public decimal Value
      {
          get
          {
              return this.valueField;
          }
          set
          {
              this.valueField = value;
          }
      }
  }
  </code></pre>
  
  <p>With XSD</p>
  
  <pre><code>&lt;xs:complexType name="ElementX"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="ElementY" minOccurs="0" maxOccurs="unbounded"/&gt;
  
  
  &lt;xs:element name="ElementY" type="ElementYType"/&gt;
  &lt;xs:element name="FieldAmount" type="AnyAmtType"/&gt;
  
  
  &lt;xs:complexType name="ElementYType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Field2" minOccurs="0"/&gt;
              &lt;xs:element ref="FieldAmount" minOccurs="0"/&gt;
              &lt;xs:element ref="Field3" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="Field1" type="xs:NMTOKEN" use="required"/&gt;
      &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>For 2</p>
  
  <pre><code>[System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public partial class ElementP
  {
      public ElementQ ElementQName { get; set; }
  }
  
  [Serializable]
  [DesignerCategory("code")]
  [XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public class ElementQ
  {
  
      public PercentageRateType Rate { get; set; }
  
  }
  
      [Serializable]
  [DesignerCategory("code")]
  [XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
  public class PercentageRateType
  {
  
     [XmlAttribute]
      public string RateUnit { get; set; }
  
      [XmlText]
      public decimal Value { get; set; }
  
  }
  </code></pre>
  
  <p>They look fine to me, what's wrong with those?</p>