Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Load XML files from Directory into Combobox

combobox xml xml files

  • Please log in to reply
1 reply to this topic

#1 jgraham95


    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 29 November 2010 - 02:29 PM

Hi there. Im looking for some information. Ive got a XML file that has some different fields. Ie Surname and Forename. Each user has a different xml file.

What im trying to do is have the combobox to auto detect all the xml files and when one is clicked on it puts the info ie surname and forename into label1 and label2.

If been looking all over the place to do this but ive had no luck. Does any1 know how 2 do this?

thanks in advance
  • 0

#2 sam_coder


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 08 December 2010 - 05:47 AM

Yup, I have some ideas.

First you need to get the XML files in the directory. This can be done pretty easily.

Something to the effect of.... (i haven't tested this , this is just a brain **..)

foreach (string xml_file_path in System.IO.Directory.GetFiles("path/to/xml-files", "*.xml")) {
     System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();

     string first_name = xdoc["person"]["name_first"].InnerText.Trim();
     string last_name = xdoc["person"]["name_last"].InnerText.Trim();

     //add this information to your combo box


The way I presented pulling information from an XML document is not considered to be the most efficient. This creates an XML object model, loading the entire XML document in memory.

the framework also offers forward only readers, which are much more efficient.

And if you run into complicated XML files, you might want to look into the benefits of XPath.

Edited by Roger, 08 December 2010 - 09:56 AM.
added code tags

  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download