title: Create XML based on condition Linq to XML
body: <p>Fairly new with Linq to XML and scratching my head over this one. I am creating an XML document using Xdocument by getting some data from a database and then usng a for each statement and adding each data line i.e.</p>
  
  <pre><code>    Dim MyCustomers As IQueryable(Of Customer) = GetCUstomers
      Dim XDoc As XDocument = &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&lt;Customers&gt;&lt;/Customers&gt;
  
      For Each c As Customer In MyCustomers
      Xdoc.Root.Add(&lt;CustomerID=&lt;%= c.Id %&gt; 
       &lt;FirstName&gt;&lt;%= c.Name %&gt;&lt;/FirstName&gt;
       &lt;Surname&gt;&lt;%= c.Surname %&gt;&lt;/Surname&gt;
      Next
  </code></pre>
  
  <p>I would like to have an If statment so if Surname is empty i want that element to be removed completely.</p>
  
  <p>So if a customer's surname exists the XML is</p>
  
  <pre><code>&lt;CustomerID=123&gt; 
  &lt;FirstName&gt;Name&lt;/FirstName&gt;
  &lt;Surname&gt;Surname&gt;&lt;/Surname&gt;
  </code></pre>
  
  <p>If not</p>
  
  <pre><code>&lt;CustomerID=123&gt; 
  &lt;FirstName&gt;Name&lt;/FirstName&gt;
  </code></pre>
  
  <p>I have tried using</p>
  
  <pre><code>&lt;%= If (string.IsnullEmpty(c.Surname) Then %&gt;
  </code></pre>
  
  <p>and other combinations, but whatever i try i get a syntax error. Ive searched MSDN and most examples refer to getting nodes but not creating nodes/elements in an XML file based on a condition. Im really lost in how to approach this one?</p>
  
  <p>Could anyone assist?</p>