Click here to Skip to main content
15,888,521 members

How to extract the name property from a selectedItem containing xml data and convert it into string.

Abhinav_Sharma asked:

Open original thread
Hello friends,

I want to extract the name property from selected item and pass it to a Elements(Xname) property,but don`t know how to convert it.Actually iam working on a filter consisting of two combo boxes and a texbox. Combobox1 is populated with selectedItem`s element(from xmldata) and combobox2 is contains selecteditem`s(xmldata) attribute.

HTML
 <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" x:Name="stc" >
                        <TextBlock Text="{Binding Path=Name}" Margin="0,0,3,0" x:Name="abc"/>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox1" ItemsSource="{Binding}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox2" ItemsSource="{Binding XPath=@*}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock  Text="{Binding Path=Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox3">
                            <ComboBoxItem Content="Equals"/>
                            <ComboBoxItem Content="Greater Than"/>
                            <ComboBoxItem Content="Less Than"/>
                        </ComboBox>
                        <TextBox Margin="0,0,4,0" Width="50" x:Name="textbox2" />
                        <Button x:Name="but1" Height="25" Width="35" Click="click" Content="gen" />
    </StackPanel>
</DataTemplate>
</ListView.ItemTemplate>



The C# Code

C#
 private void click(object sender, RoutedEventArgs e)
{
    try
    {
        var button = sender as Button;
        var parent = button.Parent as FrameworkElement;
        var attributebox = parent.FindName("cbox2") as ComboBox;
        var textbl = parent.FindName("abc") as TextBlock;
        var elementbox = parent.FindName("cbox1") as ComboBox;
        var combobx = parent.FindName("cbox3") as ComboBox;
        var textBox1 = parent.FindName("textbox2") as TextBox;
        XElement ele = XElement.Load(txtFileName.Text);
        var fil = from item in ele.DescendantsAndSelf(textbl.Text).Elements(elementbox.SelectedItem.ToString())
                  select item;//Attribute(attributebox.SelectedItem.ToString()).Value;
        foreach (var f in fil)
        {
            Label lb = new Label();
            lb.Content = f;
            canvas1.Children.Add(lb);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message,"Invalid value");
    }
}


All I want is to convert the selected combobox item to string,As i know the selectedItem properties contains object, and i want to extract the name property from the and as i cannot convert the whole object to a string....so please suggest me a way......
Tags: C# (C# 4.0), XML, WPF, LINQ, Combobox, Binding

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900