1

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.

2 Answers 2

1

You're comparing the attribute itself with the value, instead of the attribute's value.

Additionally, I can't remember offhand what the type of ListBox.SelectedItem is, but if it's object then that will be doing a reference comparison instead of equality.

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
    if (xe.Attribute("name").Value == selected)
    {
        textbox1.Text = xe.Element("SnippetCode").Value;
    }
}

Note that this will fail with an exception if there are any snippets without a "name" attribute. That's probably a good thing if every snippet is meant to have a name attribute - but if they're allowed not to, then using the explicit string conversion instead of the Value property is simple:

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
    if ((string) xe.Attribute("name") == selected)
    {
        textbox1.Text = xe.Element("SnippetCode").Value;
    }
}

Note that you can also do this through LINQ:

string selected = (string) listBox1.SelectedItem;
XDocument doc = XDocument.Load(Application.StartupPath + "\\Snippets.xml");
string code = doc.Elements("Snippets")
                 .Elements("Snippet")
                 .Where(x => x.Attribute("name").Value == selected)
                 .Select(x => x.Element("SnippetCode").Value)
                 .FirstOrDefault();

if (code != null)
{
    textbox1.Text = code;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I just realized this now.
Just tested the second piece of code and it works, thanks. Kind of a silly mistake I made.
0

I figured out the problem, xe.Attribute("name") was returning name="abc" instead of just abc. My bad for not realzing this just after making the above post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.