title: In C#, how can I serialize a POCO to XML with a lot of fancy markup?
body: <p>To consume a SOAP service, I need to send messages in XML which looks like:</p>
  <pre><code>&lt;soap:Envelope 
      xmlns:soap=&quot;http://www.w3.org/2003/05/soap-envelope&quot; 
      xmlns:bus=&quot;http://ws.praxedo.com/v6/businessEvent&quot;&gt;
      
      &lt;soap:Header/&gt;
      &lt;soap:Body&gt;
          &lt;bus:listAttachments&gt;
                  &lt;businessEventId&gt;00044&lt;/businessEventId&gt;
          &lt;/bus:listAttachments&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  <p>Obviously, the easy way to do this would just be to create a string and interpolate some variable (e.g. <code>businessEventId</code>) into the midst of it, like:</p>
  <pre><code>$&quot;&lt;soap:Envelope xmlns:soap=\&quot;http://www.w3.org/2003/05/soap-envelope\&quot;&quot;
  + &quot; xmlns:bus=\&quot;http://ws.praxedo.com/v6/businessEvent\&quot;&gt;&quot;
  + &quot;&lt;soap:Header/&gt;&lt;soap:Body&gt;&quot;
  + &quot;&lt;bus:listAttachments&gt;&lt;businessEventId&gt;{businessEventId}&lt;/businessEventId&gt;&quot;
  + &quot;&lt;/bus:listAttachments&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;&quot;
  </code></pre>
  <p>But I'd rather instantiate some collection of POPO, like:</p>
  <pre><code>public class Envelope {
     public Header Header {get; init;}
     public Body Body {get; init;}
  }
  
  public class Header {
  }
  
  public class Body {
     public ListAttachments Attachments {get; init;}
  }
  
  public class ListAttachments {
     public string BusinessEventId {get; init;}
  }
  </code></pre>
  <p>What Attributes do I need to declare?<br />
  And what would I then need to do to serialize this, assuming I have a populated instance already?</p>
  <p>--</p>
  <p>As per @DavidBrowne's comment, I've tried the following which does NOT work:</p>
  <pre><code>        private string CreateBody(string businessEventId)
          {
              BusinessEventAttachmentListRequestEnvelope envelope = BusinessEventAttachmentListRequestEnvelope.From(businessEventId);
  
              MemoryStream memorystream = new();
              DataContractSerializer serializer = new(typeof(BusinessEventAttachmentListRequestEnvelope));
              serializer.WriteObject(memorystream, envelope);
  
              memorystream.Seek(0, SeekOrigin.Begin);
  
              using StreamReader streamReader = new(memorystream);
              return streamReader.ReadToEnd();
          }
  
      [DataContract(Name = &quot;Envelope&quot;, Namespace = &quot;http://www.w3.org/2003/05/soap-envelope&quot;)]
      public class BusinessEventAttachmentListRequestEnvelope
      {
          [DataMember]
          EmptySoapHeader Header = new();
  
          [DataMember]
          BusinessEventAttachmentListRequestBody Body { get; init; }
  
          public static BusinessEventAttachmentListRequestEnvelope From(string businessEventId) =&gt;
              new()
              {
                  Body = new()
                  {
                      Request = new()
                      {
                          BusinessEventId = businessEventId
                      }
                  }
              };
      }
  
      [DataContract(Name = &quot;Header&quot;, Namespace = &quot;http://www.w3.org/2003/05/soap-envelope&quot;)]
      public class EmptySoapHeader
      {
      }
  
      [DataContract(Name = &quot;Body&quot;, Namespace = &quot;http://www.w3.org/2003/05/soap-envelope&quot;)]
      public class BusinessEventAttachmentListRequestBody
      {
          [DataMember(Name = &quot;listAttachments&quot;)]
          public BusinessEventAttachmentListRequest Request { get; init; }
      }
  
      [DataContract(Name = &quot;listAttachments&quot;, Namespace = &quot;http://ws.praxedo.com/v6/businessEvent&quot;)]
      public class BusinessEventAttachmentListRequest
      {
          [DataMember(Name = &quot;businessEventId&quot;)]
          public string BusinessEventId { get; init; }
      }
  
  </code></pre>
  <p>The resulting XML seems substantially different:</p>
  <pre><code>&lt;Envelope xmlns=\&quot;http://www.w3.org/2003/05/soap-envelope\&quot; xmlns:i=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;&gt;
      &lt;Body&gt;
          &lt;listAttachments xmlns:a=\&quot;http://ws.praxedo.com/v6/businessEvent\&quot;&gt;
              &lt;a:businessEventId&gt;00044&lt;/a:businessEventId&gt;
          &lt;/listAttachments&gt;
      &lt;/Body&gt;
      &lt;Header/&gt;
  &lt;/Envelope&gt;
  </code></pre>
  <p>And the remote server returns error 500.</p>