If you want to get the name of a vegetable from a document, the easiest way is to define it as a separate separate piece of data. There is nothing invalid about what you did, but it made it much harder to get the data you need.
If you could, change the structure to something like this to make your life easier:
<vegetables> <vegetable> <name>Carrot</name> <recipe> <name>ABCrecipe</name> <path>C:\\</path> </recipe> <recipe> <name>DEFrecipe</name> <path>D:\\</path> </recipe> </vegetable> </vegetables>
Assuming you are using .NET 3.5 or later, you will gain access to the LINQ-to-XML API. They provide a simplified way to read values from an XML document and should make it easier to solve this problem.
You create a document using:
var document = XDocument.Load("Recipe_List.xml");
Then you can write a query to get plant elements like this:
var vegetables = document .Element(XName.Get("vegetables")) .Elements(XName.Get("vegetable"));
After you have these elements, you can get their names, for example:
var vegNames = vegetables.Select(ele => ele.Element(XName.Get("name")).Value);
You can easily connect this information to your combo-box:
foreach (string name in vegNames) { comboBox1.Items.Add(name); }
source share