title: How to properly search xml document using LINQ C#
body: <p>I have a hard time time figuring out how to properly search an xml document. I have been reading the other forms like crazy today but just can't seem to understand it.  Was hopeing someone could give me a little more detailed information on how to do this the correct way and why using LINQ.  Here is the XML file.</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;body&gt;
    &lt;Customers&gt;
      &lt;Client&gt;
        &lt;Firstname Value=&quot;someguy&quot; /&gt;
        &lt;LastName Value=&quot;test&quot; /&gt;
        &lt;PhoneNumber Value=&quot;541555555&quot; /&gt;
        &lt;Address Value=&quot;55 nowhere&quot; /&gt;
        &lt;City Value=&quot;sometown&quot; /&gt;
        &lt;State Value=&quot;somestate&quot; /&gt;
      &lt;/Client&gt;
    &lt;/Customers&gt;
  &lt;/body&gt;
  </code></pre>
  <p>What I am tyring to accomplish is to return all of the values of each element that matches by a name of a customer. Here is my code.</p>
  <pre><code>       IEnumerable&lt;XElement&gt; test = doc.Root.Descendants()
              .Where(nodeName =&gt; nodeName.Name == &quot;Client&quot;
              &amp;&amp; nodeName.Descendants().Any(x =&gt; x.Name == &quot;Firstname&quot;
              &amp;&amp; x.Value == &quot;someguy&quot;));
  
          foreach (XElement m in test)
          {
              MessageBox.Show(m.ToString());
          }
  </code></pre>
  <p>Would really appreciate the help.  Please also if possible explain what the idea of using LINQ is like the format if you will. Not really sure how to explain what I am asking but for the most part just more understanding of the way it works or format etc...</p>
  <p><strong>EDIT</strong></p>
  <p>I have tried the solution given and still nothing is seeming to work.  Please show me what I am doing wrong here.</p>
  <pre><code>        private void button2_Click(object sender, EventArgs e)
      {
          string seach = txtSearch.Text;
  
          XDocument doc = XDocument.Load(@&quot;C:\users\tim\desktop\test.xml&quot;);
  
          var result = doc.Elements(&quot;Customers&quot;)
              .Elements(&quot;Client&quot;)
              .Where(x =&gt; x.Elements(&quot;Firstname&quot;)
                           .Where(c =&gt; c.Attribute(&quot;Value&quot;).Value == &quot;someguy&quot;)
                           .Any())
              .ToList();
  
          foreach (var m in result)
          {
              MessageBox.Show(m.ToString());
          }
      }
  </code></pre>
  <p><strong>EDIT:</strong></p>
  <p>Okay so I have gotten it to work now and output the data that I was looking for. Can someone please tell me if there is a more efficent way of doing what I have posted below this edit.</p>
  <pre><code>        private void button2_Click(object sender, EventArgs e)
      {
          string seach = txtSearch.Text;
  
          XDocument doc = XDocument.Load(@&quot;C:\users\tim\desktop\test.xml&quot;);
  
          var result = (from clientNode in doc.Root.Descendants(&quot;Client&quot;)
                        from name in clientNode.Descendants(&quot;Firstname&quot;)
                        where name.Attribute(&quot;Value&quot;).Value == &quot;someguy&quot;
                        select new
                        {
                            Fname = clientNode.Element(&quot;Firstname&quot;).Attribute(&quot;Value&quot;).Value,
                            Lname = clientNode.Element(&quot;LastName&quot;).Attribute(&quot;Value&quot;).Value,
                            Phone = clientNode.Element(&quot;PhoneNumber&quot;).Attribute(&quot;Value&quot;).Value,
                            Address = clientNode.Element(&quot;Address&quot;).Attribute(&quot;Value&quot;).Value,
                            City = clientNode.Element(&quot;City&quot;).Attribute(&quot;Value&quot;).Value,
                            State = clientNode.Element(&quot;State&quot;).Attribute(&quot;Value&quot;).Value
                        });
  
          foreach (var m in result)
          {
              MessageBox.Show(m.Fname + &quot;\n&quot; +
                              m.Lname + &quot;\n&quot; +
                              m.Phone + &quot;\n&quot; +
                              m.Address + &quot;\n&quot; +
                              m.City + &quot;\n&quot; +
                              m.State);
          }
  </code></pre>