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 fa开发者_Go百科r 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.
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.
You are adding a single item which is the concatenation of two XmlNodeList
s. 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...
精彩评论