Question

I have a XML file with my data in it. I want to populate the drop down options in a combobox with 2 of the fields in that xml file - FirstName and LastName.

In the xml document I am using GUID for the unique ID format, so the combobox dropdown would need the FirstName + LastName for each unique GUID variable.

What I have so far is the following:

XmlDocument xmlReturnDoc = new XmlDocument();
xmlReturnDoc.Load("Data.xml");

XmlNodeList firstname = xmlReturnDoc.GetElementsByTagName("FirstName");
XmlNodeList lastname = xmlReturnDoc.GetElementsByTagName("LastName");

StudentSelectStudentComboBox.Items.Add(firstname + lastname);

This does not work... Any help would be greatly appreciated.

Was it helpful?

Solution

Since you'll probably have to do other stuff with the information later on, I would create a custom data storage class with all the required fields, then extract the information from the XML into a collection of those custom classes. To do the display, all you need to do is add the items to the list (or data bind, whichever you prefer), and override ToString on the custom class.

OTHER TIPS

You are adding a single item which is the concatenation of two XmlNodeLists. So your ComboBox likely has only a single item in it, right?

You probably want to iterate over the lists and add each name separately. I'm not too sure this is robust the way you do it now. Basically there can be different numbers of first and last names. And matching those might be nontrivial.

You should probably iterate over the parent element of FirstName and LastName and pick out the nodes from there. That should be a better solution.

Another option would be to use the ReadXML function of Dataset, and get the entire xml file into the dataset.

Iterate through this dataset and add the required columns...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top