I am trying to get an XML element in a textbox from the users choosen combox element also from the same XML file.
I am using WPF, i am able to populate the combobox with the elements from the xml file using the following code
<ComboBox Grid.Column="1" Height="21" HorizontalAlignment="Left" Margin="0,32,0,0" Name="QueryChooser" VerticalAlignment="Top" Width="189" ItemsSource="{Binding}" SelectionChanged="QueryChooser_SelectionChanged" />
My xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Queryslistload();
}
private void Queryslistload()
{
var xElem = XElement.Load(@"Querys.xml");
var querys = from query in xElem.Descendants("QueryLay")
orderby query.Element("QueryName").Value
select query.Element("QueryName").Value;
QueryChooser.ItemsSource = querys;
}
this is my xml file itself
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Querys>
<QueryLay>
<QueryID>
1
</QueryID>
<QueryName>Check Logspace</QueryName>
<Query>dbcc sqlperf(logspace)</Query>
</QueryLay>
<QueryLay>
<QueryID>
2
</QueryID>
<QueryName>Check Spaceused</QueryName>
<Query>sp_spaceused</Query>
</QueryLay>
</Querys>
so now if the user selects the check logspace from combobox i want the query element to be displayed in the textbox
how do i achieve this?
UPDATED
public class Query
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
private void QueryChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var xElem = XElement.Load(@"Querys.xml");
var querys = xElem.Descendants("QueryLay").Select( e =>
new Query{
Id = Convert.ToInt32(e.Element("QueryID").Value),
Name = e.Element("QueryName").Value,
Value = e.Element("Query").Value
}).OrderBy(q=>q.Name)
select query.Element("QueryName").Value ;
listBox1.ItemsSource = querys;
}