title: How do I ignore if value is NULL when matching xml
body: <p>I have a method where I'm matching to XML files. Both XML's has the same ClientRefNr. But I'm trying to get all information from both and match it to one XML. That works fine. but sometimes, the Doc1 has some IDs that doesn't exist in Doc2 (NULL). And that is what I need to just jump over and continue. </p>
  
  <p><strong>XML1:</strong> </p>
  
  <pre><code> &lt;Table1&gt;
      &lt;Id&gt;1581016&lt;/ClientRefNr&gt;
      &lt;Id&gt;12&lt;/Id&gt;
      &lt;Value&gt;All commentary&lt;/Value&gt;
    &lt;/Table1&gt;
    &lt;Table1&gt;
      &lt;ClientRefNr&gt;754030&lt;/ClientRefNr&gt;
      &lt;Id&gt;17&lt;/Id&gt;
      &lt;Value&gt;All commentary&lt;/Value&gt;
    &lt;/Table1&gt;
  </code></pre>
  
  <p><strong>XML2:</strong> </p>
  
  <pre><code> &lt;Table1&gt;
      &lt;ClientRefNr&gt;1581016&lt;/ClientRefNr&gt;
      &lt;Id&gt;12&lt;/Id&gt;
      &lt;Name&gt;All commentary&lt;/Name&gt;
    &lt;/Table1&gt;
    &lt;Table1&gt;
      &lt;ClientRefNr&gt;75885&lt;/ClientRefNr&gt;
      &lt;Id&gt;11&lt;/Id&gt;
      &lt;Name&gt;Martin&lt;/Name&gt;
    &lt;/Table1&gt;
  </code></pre>
  
  <p><strong>What I tried with C#</strong></p>
  
  <pre><code>    public XDocument MatchClientCommWithLZ(XDocument ClientCommXML, XDocument doc2)
      {
          XDocument result = new XDocument();
          List&lt;XElement&gt; doc1Elements = ClientCommXML.Root.Elements().ToList();
          List&lt;XElement&gt; doc2Elements = doc2.Root.Elements().ToList();
  
  
          XElement accountElement = new XElement("ClientRefNr");
          result = new XDocument(accountElement);
  
          for (int i = 0; i &lt; doc1Elements.Count(); i++)
          {
              XmlDocument subDoc = new XmlDocument();
              subDoc.LoadXml(doc1Elements[i].ToString());
  
              string tableName = subDoc.FirstChild.Name;
              string ClientRefNr = doc1Elements[i].Elements().Where(x =&gt; x.Name == "ClientRefNr").FirstOrDefault().Value;
  
  
  
              List&lt;XElement&gt; doc1Childs = doc1Elements[i].Elements().ToList();
              List&lt;XElement&gt; doc2Childs = doc2Elements.Where(x =&gt; x.ToString().IndexOf(ClientRefNr) &gt; -1).FirstOrDefault().Elements().ToList();
  
              XElement tblElement = new XElement(tableName);
              tblElement.Add(new XElement("Id", GetClientValue(doc1Childs, doc2Childs, "Id")));
              tblElement.Add(new XElement("ClientRefNr", GetClientValue(doc1Childs, doc2Childs, "ClientRefNr")));
              tblElement.Add(new XElement("Name", GetClientValue(doc1Childs, doc2Childs, "Name")));
              tblElement.Add(new XElement("Value", GetClientValue(doc1Childs, doc2Childs, "Value")));
  
  
  
              accountElement.Add(tblElement);
  
          }
          return result;
      }
  </code></pre>
  
  <p>And that works very fine. But When I hit a <code>ClientRefNr</code> where the value does not exist in <code>Doc2</code> I get this error</p>
  
  <pre><code>An unhandled exception of type 'System.NullReferenceException' because this is NULL
  
  List&lt;XElement&gt; doc2Childs = doc2Elements.Where(x =&gt; x.ToString().IndexOf(ClientRefNr) &gt; -1).FirstOrDefault().Elements().ToList();
  </code></pre>
  
  <p>Any ideas, how to ignorer if ClientRefNr doesn't exist in Doc2</p>