XElement.Decendents.First not finding xml field
<p>I'm trying to read an XML file and find the value of a field. </p>

<p>I am reading from file "MyMessage.txt":</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;Document xmlns="urn:com.company:request.001"&gt;
    &lt;ReqHdr&gt;
        &lt;AppInstanceId&gt;AAAA&lt;/AppInstanceId&gt;
    &lt;/ReqHdr&gt;
    &lt;ReqTxInf&gt;
        &lt;PmtId&gt;
            &lt;TxId&gt;123456&lt;/TxId&gt;
        &lt;/PmtId&gt;
        &lt;MsgTyp&gt;REQUEST&lt;/MsgTyp&gt;
    &lt;/ReqTxInf&gt;
&lt;/Document&gt;
</code></pre>

<p>Here is the code:</p>

<pre><code>// Read XElement from file
element = XElement.Parse(System.IO.File.ReadAllText(
    System.IO.Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
        "MyMessage.txt")));
try
{
    Console.WriteLine(element);
    Console.WriteLine("TxId is:" + element.Descendants("TxId").First().Value);
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    Console.WriteLine(e.StackTrace);
}
</code></pre>

<p>The file is read correctly and written to the console, but the search for TxId fails.</p>

<p>I tried to repeat this this time creating the file in code, and the same code finds TxId:</p>

<pre><code>// Create XEleemnt in code
XNamespace ns = "urn:com.company:request.001";
XElement element = new XElement(ns + "Document",
    new XElement("ReqHdr", 
        new XElement("AppInstanceId", "AAAA")),
    new XElement("ReqTxInf",
        new XElement("PmtId", 
            new XElement("TxId", "123456")),
        new XElement("MsgTyp", "request")));

try
{
    Console.WriteLine(element);
    Console.WriteLine();
    Console.WriteLine("TxId is:" + element.Descendants("TxId").First().Value);
    Console.WriteLine();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    Console.WriteLine(e.StackTrace);
}
</code></pre>

<p><strong><em>Resolution</em></strong><br>
The line to read the field changed to include the name space</p>

<pre><code>Console.WriteLine("TxId is:" + element.Descendants(element.Name.Namespace + "TxId").First().Value);
</code></pre>