Rhys Clarke
Rhys Clarke

Reputation: 87

xml to populate combobox and filter the results

I have a xml file that looks like this;

<?xml version="1.0" encoding="utf-8" ?>
<Data>
  <Family>
    <ID>1</ID>
    <Head_Model>FOV</Head_Model>
    <Version>7</Version>
    <Version>15</Version>
    <Version>40</Version>
    <Version>80</Version>
    <Use>U</Use>
    <Use>M</Use>
  </Family>
  <Family>
    <ID>2</ID>
    <Head_Model>CSK</Head_Model>
    <Version>20</Version>
    <Version>40</Version>
  </Family>
  <Family>
    <ID>3</ID>
    <Head_Model>EX</Head_Model>
  </Family>
</Data>

and on my form i have 3 combo boxes, for model, version and use.

I need to populate the combo boxes with the data in the xml so if you choose "FOV" then only the versions for that should show in combo box 2 and the use in combo box 3.

I am reading the xml like this to populate combo box one at load;

public void LoadXML()
{

   using (XmlReader reader = XmlReader.Create(XmlLocation))
   {
      while (reader.Read())
      {
        if (reader.NodeType == XmlNodeType.Element)

            if (reader.Name == "ID")
            {
               reader.Read();
               string sID = reader.Value;
               string sHead_Model = string.Empty;

               if (reader.ReadToFollowing("Head_Model"))
               {
                  reader.Read();
                  sHead_Model = reader.Value;
               }


               if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty)
                  lstItems.Add(new Item(sHead_Model, Convert.ToInt32(sID)));

            }
      }
   }

   comboBox1.DataSource = lstItems;
   comboBox1.DisplayMember = "Name";
   comboBox1.ValueMember = "Id";

}

the issue is being able to filter and populate box 2 i cant figure out how to populate that with the values as I only get the first <Value> tag displayed and it does not change with the switch event.

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
   switch (comboBox1.Text)
   {
      case "FOV":

         using (XmlReader reader = XmlReader.Create(XmlLocation))
         {
            while (reader.Read())
            {
               if (reader.NodeType == XmlNodeType.Element)

                  if (reader.Name == "ID")
                  {
                     reader.Read();
                     string sID = reader.Value;
                     string sHead_Model = string.Empty;

                     if (reader.ReadToFollowing("Head_Model"))
                     {
                        reader.Read();
                        sHead_Model = reader.Value;
                        string sVersion = string.Empty;

                        if (reader.ReadToFollowing("Version"))
                        {
                           reader.Read();
                           sVersion = reader.Value;
                        }


                        if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty && sVersion != string.Empty && sHead_Model == "FOV")
                           VersionsItems.Add(new Item(sVersion, Convert.ToInt32(sID)));
                     }
                  }
            }
         }

         comboBox2.DataSource = VersionsItems;
         comboBox2.DisplayMember = "Name";
         comboBox2.ValueMember = "Id";

         break;

      case "CSK":

         using (XmlReader reader = XmlReader.Create(XmlLocation))
         {
            while (reader.Read())
            {
               if (reader.NodeType == XmlNodeType.Element)

                  if (reader.Name == "ID")
                  {
                     reader.Read();
                     string sID = reader.Value;
                     string sHead_Model = string.Empty;

                     if (reader.ReadToFollowing("Head_Model"))
                     {
                        reader.Read();
                        sHead_Model = reader.Value;
                        string sVersion = string.Empty;

                        if (reader.ReadToFollowing("Version"))
                        {
                           reader.Read();
                           sVersion = reader.Value;
                        }


                        if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty && sVersion != string.Empty && sHead_Model == "CSK")
                           VersionsItems.Add(new Item(sVersion, Convert.ToInt32(sID)));
                     }
                  }
            }
         }

         comboBox2.DataSource = VersionsItems;
         comboBox2.DisplayMember = "Name";
         comboBox2.ValueMember = "Id";

         break;

   }
}

Upvotes: 0

Views: 536

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14241

Let's create models for your data.

public class Data
{
    [XmlElement("Family")]
    public List<Family> Families { get; set; }
}

public class Family
{
    [XmlElement("ID")]
    public int Id { get; set; }

    [XmlElement("Head_Model")]
    public string HeadModel { get; set; }

    [XmlElement("Version")]
    public List<int> Version { get; set; }

    [XmlElement("Use")]
    public List<string> Use { get; set; }
}

Deserialize xml and fill model with data.
Bind comboboxes to data via BindingSource.

Data data;
var xs = new XmlSerializer(typeof(Data));

using (var stream = new FileStream("test.xml", FileMode.Open))
{
    data = (Data)xs.Deserialize(stream);
}

var bs1 = new BindingSource();
bs1.DataSource = data;
bs1.DataMember = nameof(Data.Families);

comboBox1.DataSource = bs1;
comboBox1.DisplayMember = nameof(Family.HeadModel);
comboBox1.ValueMember = nameof(Family.Id);

var bs2 = new BindingSource();
bs2.DataSource = bs1;
bs2.DataMember = nameof(Family.Version);

comboBox2.DataSource = bs2;

var bs3 = new BindingSource();
bs3.DataSource = bs1;
bs3.DataMember = nameof(Family.Use);

comboBox3.DataSource = bs3;

Of course, you can make class fields instead of local variables for bs1, bs2, bs3 and data.

Upvotes: 1

Related Questions