title: Conditional XElement Content, XElement or XAttribute
body: <p>I'm building XML based on some data pulled from a database, and thought to try building the XML with LINQ2XML. The data is coming from a few different places, so I've been building the XML by sections, based on what I have populated in DataTable objects. </p>
  
  <p>Using the XElements and XAttributes, I can get one or the other, but I was wondering if anyone had insight in how to make the determination of whether to use XElement or XAttribute at the point where the XML is being built. </p>
  
  <p>Here's a section from the XML building code with the XAttribute hard-coded (VB.NET):</p>
  
  <pre><code> ...
   New XElement("ParentNode",    
        New XElement("ChildNode",New XAttribute(XNamespace.Xmlns + "nil", "true"))
   )
   ...
  </code></pre>
  
  <p>Here's an example of what I'm using throughout the XML building process to build child nodes where necessary:</p>
  
  <pre><code>...
  New XElement("ParentNode",
       From db as DataRow in dtTable.Rows _
       Order By db("Field") _
       Select New XElement("ChildNode",
            New XElement("ChildID",db("ValueToParse"))
       )
  )
  ...
  </code></pre>
  
  <p>So it's in that LINQ query that I need to determine if ValueToParse, for example, has a valid value (non-empty). If it does, then I can add the node and it's value. If it DOES NOT, I need to add that named node, but with an attribute of 'xsi:nil="true"' </p>
  
  <p>The caveats are that that there are some descendants which repeat over different parents (for example, there's an AddressDetails section which contains AddressDetail elements. These AddressDetail elements can contain 0 or 1 Contacts groups of Contact elements, so adding a node to the Contacts node isn't a straightforward option as it would be if there was only ONE Contacts element). </p>
  
  <p>I don't usually USE LINQ, but it SEEMED like a good idea at the time XD</p>
  
  <p>Thanks,
  Chris </p>