I looked at several examples within here and it looks like I'm following the right procedure yet it still not working so I am obviously doing something wrong. I have two comboBox which I am trying to populate from an XML file The idea is that once the first selection has been made the set of choices for the secondary comboBox will be generated and if i switch the first selection then the secondary comboBox should refresh as well
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<ComboBox>
<Customer name="John"/>
<Data>
<System>Linux</System>
</Data>
<Customer name="Fernando"/>
<Data>
<System>Microsoft</System>
<System>Mac</System>
</Data>
</ComboBox>
This is my C# code
//This part works the customer_comboBox is generated correctly with John and Fernando
XmlDocument doc = new XmlDocument();
doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\comboBox.xml");
XmlNodeList customerList = doc.SelectNodes("ComboBox/Customer");
foreach (XmlNode node in customerList)
{
customer_comboBox.Items.Add(node.Attributes["name"].InnerText);
}
//putting the selected item from the first comboBox in a string
string selected_customer = customer_comboBox.SelectedItem.ToString();
//This is the part that does not work
//I want to read XML node that matches selected_customer and populate systems available
foreach (XmlNode node in customerList)
{
if (node.Attributes["name"].InnerText == selected_customer)
{
foreach (XmlNode child in node.SelectNodes("ComboBox/Customer/Data"))
{
system_comboBox.Items.Clear();
system_comboBox.Items.Add(node["System"].InnerText);
}
}
}
I gone two days straight trying to figure this out. Not sure if my XML is wrong or the way im calling the child node.
I've checked your code and I've the following changes.
The Customer node was closed and isn't nesting the Data/System nodes. This results in the following two xpaths:
By nesting the Data/System nodes inside the Customer nodes is the first part of the solution:
<?xml version="1.0" encoding="utf-8" ?>
<ComboBox>
<Customer name="John">
<Data>
<System>Linux</System>
</Data>
</Customer>
<Customer name="Fernando">
<Data>
<System>Microsoft</System>
<System>Mac</System>
</Data>
</Customer>
</ComboBox>
The change in the XML structure simplifies the xpath usage in the "node.SelectNodes()" statement to just include "Data/System". I also simplified "node["System"].InnerText" to "child.InnerText":
foreach (XmlNode node in customerList)
{
if (node.Attributes["name"].InnerText == selected_customer)
{
system_comboBox.Items.Clear();
foreach (XmlNode child in node.SelectNodes("Data/System"))
{
system_comboBox.Items.Add(child.InnerText);
}
}
}
Because the Customer list needs it's items to already exist at runtime. Add the items manually to the combobox and remove this block:
foreach (XmlNode node in customerList)
{
customer_comboBox.Items.Add(node.Attributes["name"].InnerText);
}