title: XML XElement building an xml document
body: <p>Hi I am new to XML building, I basically didn't use it before, I always preferred json.</p>
  
  <p>I have a solution where I just make it with string and convert to XML object, but how can I do it with XElement class?</p>
  
  <p>This is the document:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;requestblock version="3.67"&gt;
      &lt;alias&gt;ALIAS&lt;/alias&gt;
      &lt;request type="AUTH"&gt; 
         &lt;operation&gt; 
            &lt;sitereference&gt;test12345&lt;/sitereference&gt; 
            &lt;accounttypedescription&gt;TEST&lt;/accounttypedescription&gt; 
            &lt;parenttransactionreference&gt;12-3-4567&lt;/parenttransactionreference&gt; 
         &lt;/operation&gt; 
         &lt;merchant&gt; 
            &lt;orderreference&gt;Example recurring auth&lt;/orderreference&gt;
         &lt;/merchant&gt; 
         &lt;customer&gt; &lt;/customer&gt; 
         &lt;billing&gt; 
            &lt;amount currencycode="GBP"&gt;1234&lt;/amount&gt; 
            &lt;subscription type="TEST"&gt;
               &lt;number&gt;1&lt;/number&gt;
            &lt;/subscription&gt; 
         &lt;/billing&gt; 
         &lt;settlement/&gt;
      &lt;/request&gt; 
   &lt;/requestblock&gt;
  </code></pre>
  
  <p>I already have a part of the code like this:</p>
  
  <pre><code>       XElement address =
              new XElement("alias", "TEST",
              new XElement("request", new XAttribute("type", "AUTH"),
              new XElement("City", "Mercer Island"),
              new XElement("State", "WA"),
              new XElement("Postal", "68042")
         ));
  </code></pre>
  
  <p>But I have a problem with alias, because alias is closed after all elements, not in the same notation:</p>
  
  <pre><code>&lt;alias&gt;TEST
      &lt;request type="AUTH"&gt;
          &lt;City&gt;Mercer Island&lt;/City&gt;
          &lt;State&gt;WA&lt;/State&gt;
          &lt;Postal&gt;68042&lt;/Postal&gt;
      &lt;/request&gt;
  &lt;/alias&gt;
  </code></pre>
  
  <p>As you can see  notation is the problem.</p>