title: Find a string in XML
body: <p>I would like to know how to find a string in XML file.</p>
  
  <p>Say this is the XML file i have (these are the SQL server instances btw, irrelevant)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Servernames&gt;
      &lt;loc country="Lockheed"&gt;
          &lt;Servername&gt;instance1\server1&lt;/Servername&gt;
          &lt;Servername&gt;instance2\server2&lt;/Servername&gt;
          &lt;Servername&gt;10.90&lt;/Servername&gt;
      &lt;/loc&gt;
      &lt;loc country="SouthAmerica"&gt;
          &lt;Servername&gt;Hide your heart&lt;/Servername&gt;
          &lt;Servername&gt;Bonnie Tyler&lt;/Servername&gt;
          &lt;Servername&gt;10.0&lt;/Servername&gt;
      &lt;/loc&gt;
      &lt;loc country="Britian"&gt;
          &lt;Servername&gt;Greatest\Hits&lt;/Servername&gt;
          &lt;Servername&gt;Dolly\Parton&lt;/Servername&gt;
          &lt;Servername&gt;this\is&lt;/Servername&gt;
      &lt;/loc&gt;
  &lt;/Servernames&gt;
  </code></pre>
  
  <p>So what happens is i get a string from the user in any format say for example i only get instance and then i want the listbox to display all the servernames that start with server in the above case it will be </p>
  
  <p>instance1\server1<br>
  instance2\serve2  </p>
  
  <p>and so on..
  Not sure how to achieve this, do i have to open stream reader or just get a string and browser thru the xml file?</p>
  
  <p>UPDATED</p>
  
  <pre><code>private void button1_Click(object sender, RoutedEventArgs e)
  {
      textBox1.Clear();
      string fileName = "c:\\users\\xxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";
  
          var doc = XDocument.Load(fileName);
          var findString = "Server";
  
          var results = doc.Element("Servernames").Descendants("Servername").Where(d =&gt; d.Value.Contains(findString)).Select(d =&gt; d.Value);
          listBox1.Items.Add(results.ToString());
          textBox1.Text = results.ToString();
  }
  </code></pre>
  
  <p>i am simply getting this in the text box :<em>System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]</em></p>
  
  <p><img src="https://i.stack.imgur.com/rcJZA.jpg" alt="enter image description here"></p>
  
  <p><strong>strong text</strong>UPDATE2</p>
  
  <p>.cs file code</p>
  
  <p>private void button1_Click(object sender, RoutedEventArgs e)
          {
              textBox1.Clear();</p>
  
  <pre><code>        string fileName = "c:\\users\\xxxxx\\documents\\visual studio 2010\\Projects\\WpfApplication2\\WpfApplication2\\XML.xml";
  
          var doc = XDocument.Load(fileName);
          var findString = "Server";
  
          var results = doc.Element("Servernames").Descendants("Servername").Where(d =&gt; d.Value.Contains(findString)).Select(d =&gt; d.Value);
  
          Servers = new ObservableCollection&lt;string&gt;(results);
  
          MessageBox.Show("THis is loaded");
  
      }
  </code></pre>
  
  <p>XAML looks like this</p>
  
  <pre><code>&lt;ListBox   Height="200" HorizontalAlignment="Left" Margin="200,44,0,0" x:Name="ListBox1" VerticalAlignment="Top" Width="237"&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xt2bx.jpg" alt="enter image description here"></p>