title: Can I avoid cluttering my class with repetitive XmlElement attributes even though the root node is in a different namespace than its children?
body: <p>I am retrieving XML documents from a web service I have no control over. The XML is formatted similarly to the following:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ns:obj xmlns:ns="somenamespace"&gt;
       &lt;address&gt;1313 Mockingbird Lane&lt;/address&gt;
       &lt;residents&gt;5&lt;/residents&gt;
  &lt;/ns:obj&gt;
  </code></pre>
  
  <p>where the root node is in the "ns" namespace, but none of its child elements are.</p>
  
  <p>After some trial and error, I found that I could deserialize the document to a C# object by doing the following:</p>
  
  <pre><code>[XmlRoot(Namespace="somenamespace", ElementName="obj")]
  public class xmlObject
  {
      [XmlElement(Namespace = "")]
      public string address { get; set; }
  
      [XmlElement(Namespace = "")]
      public int residents { get; set; }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          string xml =
              "&lt;?xml version=\"1.0\"?&gt;" +
              "&lt;ns:obj xmlns:ns=\"somenamespace\"&gt;" +
              "   &lt;address&gt;1313 Mockingbird Lane&lt;/address&gt;" +
              "   &lt;residents&gt;5&lt;/residents&gt;" +
              "&lt;/ns:obj&gt;";
  
          var serializer = new XmlSerializer(typeof(xmlObject));
  
          using (var reader = new StringReader(xml))
          {
               var result = serializer.Deserialize(reader) as xmlObject;
  
               Console.WriteLine("{0} people live at {1}", result.residents, result.address);
               // Output: "5 people live at 1313 Mockingbird lane"
          }
      }
  }
  </code></pre>
  
  <p>If I omit the <code>XmlElementAttribute</code> on the individual members, I instead get an empty object. I.e. The output reads</p>
  
  <pre><code>0 people live at 
  </code></pre>
  
  <p>(<code>result.address</code> is equal to <code>null</code>.)</p>
  
  <p>I understand the rationale behind why the deserialization process works like this, but I'm wondering if there is a less verbose way to tell <code>XmlSerializer</code> that the child elements of the object are not in the same namespace as the root node.</p>
  
  <p>The objects I'm working with in production have dozens of members and, for cleanliness sake, I'd like to avoid tagging all of them with <code>[XmlElement(Namespace = "")]</code> if it's easily avoidable.</p>