I have simple window where XML nodes are loaded to the ListBox as items. Now I want to remove some nodes by choosing an item from that ListBox and clicking a button to delete. 

I've tried to store selected item to variable which will be compared if the InnerText has the same value in XML as selected item in ListBox. But something is definitely wrong, because it doesn't work.

C# code:

```cs
static string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;
static string xmlLocation = System.IO.Path.Combine(projectDirectory, "SubjectList.xml");

string selectedSubjectItem;

private void DeleteSubjectButton_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlLocation);
            foreach(XmlNode node in document.SelectNodes("Subjects/Subject"))
            {
                if (node.SelectSingleNode("Subject").InnerText == selectedSubjectItem)
                {
                    node.ParentNode.RemoveChild(node);
                }
                document.Save(xmlLocation);
            }
        }

        private void SubjectList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedSubjectItem = SubjectList.SelectedItem.ToString();
        }

```

XAML code:

```xml
    <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="SubjectData" Source="SubjectList.xml" XPath="/Subjects/Subject"/>
        </Grid.Resources>
        <DockPanel Margin="10">
            <StackPanel DockPanel.Dock="Right" Margin="10,0,0,0">
                <Button Name="AddSubjectButton" Margin="0,0,0,5" Click="AddSubjectButton_Click">Add</Button>
                <Button Name="DeleteSubjectButton" Click="DeleteSubjectButton_Click">Delete</Button>
            </StackPanel>
            <StackPanel DockPanel.Dock="Left">
                <ListBox x:Name="SubjectList" Margin="0,0,0,0" ItemsSource="{Binding Source={StaticResource SubjectData}}" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="175" SelectionChanged="SubjectList_SelectionChanged"></ListBox>
                <TextBox x:Name="InputNewSubject" Text="Input new subject..."/>
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>

```

XML file:

```xml
<?xml version="1.0" encoding="utf-8"?>
<Subjects>
  <Subject>Subject1</Subject>
  <Subject>Subject2</Subject>
  <Subject>Subject3</Subject>
</Subjects>

```