C# getting a specific attribute of a child of a Previous node in XML while testing the node for a missing element
<p>I have a <code>xliff</code> and I am trying to get the value of the <code>id</code> attribute of the last <code>x</code> element of <code>source</code> in the previous <code>trans-unit</code> so long as it does not have a <code>target</code> child.</p>

<p>So very specifically, I don't just need to return all elements, but get the parent node, test if the previous has a <code>target</code> element and if so return <code>null</code>, but if it does not have then return the value of the <code>id</code> attribute of the last child <code>x</code> of the <code>source</code> element.</p>

<p><strong>Update:</strong> There are many <code>trans-units</code> in the xliff, the below is just a snippet. I locate a specific one based on the <code>&lt;mrk mtype="seg" mid="6"&gt;</code> element (using its <code>mid</code> attribute) and then I need to get the the previous node's child if there is no target.</p>

<p>Here is the snippet of the XML so you see what I am trying to get:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;xliff xmlns:sdl="http://sdl.com/FileTypes/SdlXliff/1.0" version="1.2" sdl:version="1.0" xmlns="urn:oasis:names:tc:xliff:document:1.2"&gt;
&lt;!-- xliff goes on here --&gt;
  &lt;trans-unit id="495bbe35-ad63-4d37-91e0-8261c2e4052e" translate="no"&gt;
    &lt;source&gt;
      &lt;x id="11" /&gt;
      &lt;x id="12" /&gt;       &lt;!-- THIS IS THE ID I WANT TO GET --&gt;
    &lt;/source&gt;
  &lt;/trans-unit&gt;
  &lt;trans-unit id="5bccf5f4-56c9-4969-8416-f3114eb36e86"&gt;
    &lt;source&gt;
      &lt;x id="13" /&gt;SOME TEXT
    &lt;/source&gt;
    &lt;seg-source&gt;
      &lt;mrk mtype="seg" mid="6"&gt;     &lt;!-- THIS IS THE MID I WANT TO START FROM --&gt;
        &lt;x id="13" /&gt;SOME TEXT
      &lt;/mrk&gt;
    &lt;/seg-source&gt;
    &lt;target&gt;
      &lt;mrk mtype="seg" mid="6"&gt;
        &lt;x id="13" /&gt;SOME TRANSLATION
      &lt;/mrk&gt;
    &lt;/target&gt;
    &lt;sdl:seg-defs&gt;
      &lt;sdl:seg id="6" conf="ApprovedTranslation" origin="interactive" /&gt;
    &lt;/sdl:seg-defs&gt;
  &lt;/trans-unit&gt;
</code></pre>

<p>So in this case I want <code>"12"</code> returned. If there is a target, I should get <code>null</code>.</p>

<p>I managed to return the entire node with the following line of code, but I don't seem to be able to get to the children:</p>

<pre><code>xliff = XDocument.Load(Path.GetFullPath(FilePath));
XNamespace xmlns = "urn:oasis:names:tc:xliff:document:1.2";
XNamespace ns = "http://sdl.com/FileTypes/SdlXliff/1.0";
string testid = xliff.Descendants()
                     .Elements(xmlns + "trans-unit")
                     .Elements(xmlns + "seg-source")
                     .Elements(xmlns + "mrk")
                     .Where(e =&gt; e.Attribute("mtype").Value == "seg" &amp;&amp; e.Attribute("mid").Value == SegId)
                     .FirstOrDefault().Parent.Parent.PreviousNode.ToString();
</code></pre>

<p>This returns the previous node alright, but then further progressed with the code, I have this, but this throws an exception:</p>

<pre><code>string testid = xliff.Descendants()
                     .Elements(xmlns + "trans-unit")
                     .Elements(xmlns + "seg-source")
                     .Elements(xmlns + "mrk")
                    .Where(e =&gt; e.Attribute("mtype").Value == "seg" &amp;&amp; e.Attribute("mid").Value == SegId)
                    .Select(n =&gt; n.Parent.Parent.PreviousNode as XElement)
                    .Where(c =&gt; c.Elements(xmlns + "target") == null &amp;&amp; c.Elements(xmlns + "source").Elements(xmlns + "x").Last().Value != null)
                    .FirstOrDefault().Attribute("id").Value;
</code></pre>

<p>Can somebody help please?</p>