I would like to know how to find a string in XML file.
Say this is the XML file i have (these are the SQL server instances btw, irrelevant)
<?xml version="1.0" encoding="utf-8" ?>
<Servernames>
<loc country="Lockheed">
<Servername>instance1\server1</Servername>
<Servername>instance2\server2</Servername>
<Servername>10.90</Servername>
</loc>
<loc country="SouthAmerica">
<Servername>Hide your heart</Servername>
<Servername>Bonnie Tyler</Servername>
<Servername>10.0</Servername>
</loc>
<loc country="Britian">
<Servername>Greatest\Hits</Servername>
<Servername>Dolly\Parton</Servername>
<Servername>this\is</Servername>
</loc>
</Servernames>
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
instance1\server1
instance2\serve2
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?
UPDATED
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 => d.Value.Contains(findString)).Select(d => d.Value);
listBox1.Items.Add(results.ToString());
textBox1.Text = results.ToString();
}
i am simply getting this in the text box :System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]

strong textUPDATE2
.cs file code
private void button1_Click(object sender, RoutedEventArgs e) { textBox1.Clear();
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 => d.Value.Contains(findString)).Select(d => d.Value);
Servers = new ObservableCollection<string>(results);
MessageBox.Show("THis is loaded");
}
XAML looks like this
<ListBox Height="200" HorizontalAlignment="Left" Margin="200,44,0,0" x:Name="ListBox1" VerticalAlignment="Top" Width="237">

resultsfor ex.,String.Join("\n",results)- L.BfindString"server" instead of "Server". If you need a case insensitive search, then your "Where" becomes.Where (d => d.Value.ToLower().Contains(findString.ToLower()))- Robaticus