Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

How to populate text box with the xml's attribute's value based on the combobox's selection

Back to Forum: Tech Off
  • User profile image
    Varunkrishna

    I have a xml file with the  fields country name, pin, state, currency. The Sample xml is shown below

    .....
    <Country Name="India" State = "TN" Currency ="RS" PIN = "123456" />
    <Country Name = "USA" State = "TX" Currency ="$" PIN= "123456" />
    .........
    

    So I had shown the country names as a combo box, and it's values are populated by using the following code

    using System.Xml;
    using System.IO;
    private void showCountryDetails()
    {
    string filename = "countryDetails.xml";
    var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    path += @"\" + fileName;
    XmlDocument countryDetailsXmlDoc= new XmlDocument();
                countryDetailsXmlDoc.Load(path);
    XmlNodeList listCountryDetails = countryDetailsXmlDoc.SelectNodes("/Countries/Country");
    foreach (XmlNode node in listCountryDetails)
    {
    comboCountryName.Items.Add(node.Attributes["Name"].InnerText);
    }
    }
    

    Now all the names of the countries are populated in the combo box, assume that I am choosing India in the combo box, and I want populate all the attributes from the xml in to the text box,for example the state's value should be displayed in the state text box and the Currency value should be displayed in the Currency text box and so on.

    Could someone please help me out here ?

    Thanks

    Varun Krishna. P

  • User profile image
    CoderRajat

    ///Check out this code. This will solve your Problem
    
    private void SelectCountryDetails()
    {
    string filename = "countryDetails.xml";
    var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    path += @"\" + fileName;
    XmlDocument countryDetailsXmlDoc= new XmlDocument();
                countryDetailsXmlDoc.Load(path);
    XmlNodeList listCountryDetails = countryDetailsXmlDoc.SelectNodes("/Countries/Country");
    int i = 0;
    foreach (XmlNode node in listCountryDetails)
    {
    if(comboCountryName.Items.SelectedItem == node.Value)
    {
    ///Take out other details form the node
    }
    }
    }

  • User profile image
    spivonious

    @CoderRajat: Or better yet, capture all of the details when you populate the dropdown and store them in a Dictionary using the country name as key.

    Then you can just say countryDetails["India"] instead of parsing the XML file each time.

  • User profile image
    JohnAskew

    , spivonious wrote

    @CoderRajat: Or better yet, capture all of the details when you populate the dropdown and store them in a Dictionary using the country name as key.

    Then you can just say countryDetails["India"] instead of parsing the XML file each time.

    +1 !

    When you fill the drop down, create a dictionary with country as key, and all the rest in a custom object that you can push into other controls...

    public class CountryDetails {

      public string State { get; set; }

      public string Currency { get; set; }

      public string Pin { get; set }

    }

    Dictionary<string, CountryDetails> ddlData = 
    new Dictionary<string, CountryDetails>();

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.