title: Using XMLSerializer to add attributes to a Class Member
body: <p>I am attempting to write a WCF service which uses XmlSerialzer to output the xml. I need a specific xml output which is why I am not using DataContract Seriailzer. Specifically I am writing a catalogue service web(csw) which has a defined schema etc.</p>
  
  <p>I have been trying to write the classes first and then test what XML is being outputted. This is rather tedious and I may switch to the xsd utility. What I would like to know is can I add xml attributes to other class members or will those decorated xmlattributes only be added to the root element i.e the class name? There seems to be flexibility issues compared to writing the CML by hand using XDocument. Also every time I need to nest elements (not collections) It seems I need to create a new class? Is that right?</p>
  
  <p>The output xml I need is:</p>
  
  <pre><code>&lt;ows:ContactInfo&gt; 
  &lt;ows:OnlineResource 
     xlink:href="mailto:­­enquiry@gis.nottscc.gov.uk­­"/&gt; 
  &lt;/ows:ContactInfo&gt;
  </code></pre>
  
  <p>Here is my class:</p>
  
  <pre><code>   public class ContactInfo
         {
              [XmlElement] 
              public string OnlineResource = "";        
  
              [XmlElementAttribute(ElementName = "OnlineResource",Namespace = "http://www.w3.org /1999/xlink")]
              public string href = "mailto:enquiry@gis.nottscc.gov.uk";
  
           }
  </code></pre>
  
  <p>which outputs the xml as follows:</p>
  
  <pre><code>&lt;ows:ContactInfo xlink:href="mailto:enquiry@gis.nottscc.gov.uk"&gt;
  &lt;ows:OnlineResource&gt;mailto:enquiry@gis.nottscc.gov.uk&lt;/ows:OnlineResource&gt;
  &lt;/ows:ContactInfo&gt;
  </code></pre>