title: Null reference exception - XDocument using Element and XPath
body: <p>I am getting some XML data in the form of a string from a database. The data is saved as ntext in the database.</p>
  
  <p>Getting the data from the database is not a problem. The problem is later, when I want to handle the data in the xml. I load a string into an xDocument.</p>
  
  <p>I want to get the Owner value at first. But I get a nullreference exception, meaning that I do not write the correct Xpath I assume. </p>
  
  <p>Writing "./Owner" does not work. Writing "/./Owner" does not work I then get an XML exception. </p>
  
  <p>I started out with XMLDocument but think I ran into a namespace problem. Started reading and looks like using xDocument is better.
  As you also can see by my code, I have tried getting the Owner value in two ways, both fail.</p>
  
  <p>My XML looks a bit like this:</p>
  
  <pre><code>    &lt;Container xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="DM"&gt;
  &lt;IsConsigned&gt;false&lt;/IsConsigned&gt;
  &lt;LockState&gt;Unlocked&lt;/LockState&gt;
  &lt;SourceType i:nil="true" /&gt;
  &lt;Id&gt;04216194-4f62-47ee-ab21-c1053d01bf1e&lt;/Id&gt;
  &lt;Owner&gt;IN&lt;/Owner&gt;
  &lt;Created&gt;2012-08-21T09:29:10.528321+02:00&lt;/Created&gt;
  &lt;/Container&gt;
  </code></pre>
  
  <p>And my code:</p>
  
  <pre><code>      class Program
  {
     static SqlConnection conn = new SqlConnection();
     static XDocument xml = new XDocument();
  
      static void Main(string[] args)
      {
          using (conn)
          {
              conn.ConnectionString = Properties.Settings.Default.connectionString;
              //connection.Open();
              conn.Open();
              SqlDataReader reader = GetDataFromDatabase();
  
              if (reader.HasRows)
              {
  
                  while (reader.Read())
                  {
                      string xmlFile = reader.GetSqlString(0).ToString();
                      HandleData(xmlFile);
                  }
              }
              else
              {
                  Console.WriteLine("No rows found.");
              }
              reader.Close();
          }
      }
  
      public static void HandleData(string xmlIn)
      {
  
          xml = XDocument.Parse(xmlIn);
          XElement xmlElement = xml.Root;
          string test1 = xml.Element("Owner").Value.ToString();
          string test = xmlElement.Element("Owner").Value.ToString();
      }
  
      }
  </code></pre>