Here is some sample XML I'm using for this solution.
="1.0"="utf-8"
<data>
<comboBox id="comboBox1">
<item displayName="ComboBox1 Test 1" value="ComboBox1 Test 1" selected="false" />
<item displayName="ComboBox1 Test 2" value="ComboBox1 Test 2" selected="true" />
<item displayName="ComboBox1 Test 3" value="ComboBox1 Test 3" selected="false" />
<item displayName="ComboBox1 Test 4" value="ComboBox1 Test 4" selected="false" />
<item displayName="ComboBox1 Test 5" value="ComboBox1 Test 5" selected="false" />
</comboBox>
<comboBox id="comboBox2">
<item displayName="ComboBox2 Test 1" value="ComboBox1 Test 1" selected="false" />
<item displayName="ComboBox2 Test 2" value="ComboBox1 Test 2" selected="false" />
<item displayName="ComboBox2 Test 3" value="ComboBox1 Test 3" selected="true" />
<item displayName="ComboBox2 Test 4" value="ComboBox1 Test 4" selected="false" />
<item displayName="ComboBox2 Test 5" value="ComboBox1 Test 5" selected="false" />
</comboBox>
<comboBox id="comboBox3">
<item displayName="ComboBox3 Test 1" value="ComboBox1 Test 1" selected="false" />
<item displayName="ComboBox3 Test 2" value="ComboBox1 Test 2" selected="false" />
<item displayName="ComboBox3 Test 3" value="ComboBox1 Test 3" selected="false" />
<item displayName="ComboBox3 Test 4" value="ComboBox1 Test 4" selected="true" />
<item displayName="ComboBox3 Test 5" value="ComboBox1 Test 5" selected="false" />
</comboBox>
</data>
So the first step is to load the data from the XML file and bind it to the ComboBox objects on the form.
private void loadButton_Click(object sender, EventArgs e)
{
var comboBoxData = XDocument.Load("ComboBoxData.xml");
foreach (var element in comboBoxData.Element("data").Elements("comboBox"))
{
ComboBox comboBox =
(ComboBox)(Controls.Find(element.Attribute("id").Value, false)).First();
comboBox.DataSource =
(from data in element.Elements("item")
select new ComboBoxItem
{
ItemDisplayName = data.Attribute("displayName").Value,
ItemValue = data.Attribute("value").Value
}).ToList();
comboBox.DisplayMember = "ItemDisplayName";
comboBox.ValueMember = "ItemValue";
comboBox.SelectedValue =
(from data in element.Elements("item")
where data.Attribute("selected").Value == "true"
select data.Attribute("value").Value).First();
}
}
I use an XDocument to load the data from the file. If your files get too large, this won't be an efficient way to process this data. Next step is to loop through all of the ComboBox objects on the form. Using the ID of the control stored in the XML file, I'm able to find the control on the form and work with it directly. I use LINQ projection to build object list that I can bind to the ComboBox. I set up the display name and value members and I also select the item to display where the attribute "selected" has "true".
Now all of your ComboBox objects are bound to the XML file, the next fun part is to save their state.
private void saveButton_Click(object sender, EventArgs e)
{
var comboBoxData = new XDocument();
var dataRootElement = new XElement("data");
foreach (Control control in this.Controls)
{
if (control is ComboBox)
{
var comboBoxElement = new XElement("comboBox");
comboBoxElement.SetAttributeValue("id", control.Name);
for (int index = 0; index < ((ComboBox)control).Items.Count; index++)
{
ComboBoxItem item =
(ComboBoxItem)((ComboBox)control).Items[index];
var itemElement = new XElement("item");
itemElement.SetAttributeValue("displayName", item.ItemDisplayName);
itemElement.SetAttributeValue("value", item.ItemValue);
itemElement.SetAttributeValue("selected",
((ComboBox)control).SelectedIndex == index ? "true" : "false");
comboBoxElement.Add(itemElement);
}
dataRootElement.Add(comboBoxElement);
}
}
comboBoxData.Add(dataRootElement);
comboBoxData.Save("ComboBoxData.xml");
}
This is actually easier then it sounds. I use the XDocument and XElement objects to construct each XML element for the document. I use attributes to store the state of the object.
Hopefully this gives you a good start!