Tech Off Post

Single Post Permalink

View Thread: How to populate text box with the xml's attribute's value based on the combobox's selection
  • 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