title: VB.NET add an element to the XML document with LINQ to XML
body: <p>I'm adding an element to existing XML doc with the following code:</p>
  
  <pre><code>        Dim theXMLSource As String = Server.MapPath("~/Demo/") &amp; "LabDemo.xml"
      Dim nodeElement As XElement
  
      Dim attrAndValue As XElement = _
          &lt;LabService&gt;
              &lt;ServiceType&gt;
                  &lt;%= txtServiceType.Text.Trim %&gt;
              &lt;/ServiceType&gt;
              &lt;Level&gt;
                  &lt;%= txtLevel.Text.Trim %&gt;
              &lt;/Level&gt;
          &lt;/LabService&gt;
  
      nodeElement.Add(New XElement(attrAndValue))
      nodeElement.Save(theXMLSource)
  </code></pre>
  
  <p>It makes error like this:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  
  Object reference not set to an instance of an object.
  
  Error line: nodeElement.Add(New XElement(attrAndValue))
  </code></pre>
  
  <p>I debugged it but I couldn't get the error yet. Can you show what the problem is? Thank you</p>