I have an XML file formatted like this:
<?xml version="1.0" encoding="utf-8"?>
<Snippets>
<Snippet name="abc">
<SnippetCode>
testcode1
</SnippetCode>
</Snippet>
<Snippet name="xyz">
<SnippetCode>
testcode2
</SnippetCode>
</Snippet>
...
</Snippets>
I have populated a listbox with the snippet name, and it works fine so far. For example (I haven't added any real snippets yet btw), my listbox contains:
abc
xyz
123
When I click on an item in the listbox, I want the snippet code of that item to be inserted into a textbox. Like if abc was clicked, testcode1 should be inserted into the textbox. I used this code on the double click event:
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
if (listBox1.SelectedItem == xe.Attribute("name"))
{
textbox1.Text = xe.Element("SnippetCode").Value;
}
}
However, nothing gets inserted because it never finds the snippet code value. I added a MessageBox.Show("test"); inside the if statement to check if it executes but it never does. The selected listbox item name and snippetname have the same text, so it's quite strange it isn't ever executing.
Does anyone know what's wrong with my code? Also, does anyone know of a better idea to insert text in the document from the snippet element? This method isn't probably good as performance might be a problem for large XML files.