XML :
<?xml version="1.0" encoding="utf-8"?>
<servers>
<server>s2
<database>db1</database>
<database>db2</database>
</server>
<server>s3
<database>db3</database>
<database>db4</database>
</server>
</servers>
I want to get database based on server name & return as a list of string type.
view :
This function will take server name as a parameter from first combo box and based on the will filter database and populate second combobox
public List<string> GetDatabases(string serv)
{
var item = from items in xdoc.Descendants("database")
where (string)items.Element("server") == serv
select items.Elements("database").ToList();
foreach (var items in item)
{
lstDBName.Add(items.ToString());
}
return lstDBName;
}
The XML you posted is not valid. If you test it with an XML validator you will see it gives this error:
You can easily fix it by adding an attribute to store server name like this:
And you can modify (and simplify a bit) your code like this:
I read the XML inside the method from a file called XMLFile1.xml, you can change that bit with however you're reading the XML.
This code sample would return a list of two strings with values "db1" and "db2" as expected
As I can understand, you need some mechanism which on selection in first combo will update the second combo with first combo related data (based on your question name:
).
Here is a solution which is based on two observable collections an its update mechanisms. 1. Xaml code:
2. Main Model Code which gives the solution.The UpdateDbCollection method can call your GetDatabases method, query server related data bases, put that data to the 'newDbs' list and update the second combo observable collections with new data in way when it add a new data and remode an old data.
3. Models:
View model code:
public class GridViewModel:BaseObservableObject {
}
BaseObservableObject is a simple implementation of INotifyPropertyChanged.
I hope it will help you. Thanks an regards,