使用xml文件中的值填充combobox的代码

如何使用xml文件中的值填充combobox。

您必须从文件中读取数据,然后可以使用dataset.ReadXML()之类的东西,然后使用它来设置combobox的绑定。

这是一个让你入门的例子。 http://www.codeproject.com/KB/cs/dropdownfromxml.aspx

更新:请注意,有两个DataGrid类。 具有DataBind()方法的那个在System.Web.UI.WebControls命名空间中。 Windows窗体控件不具备DataBind方法,并且应该在没有该行的情况下工作。 请参阅: http : //msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.datasource.aspx

使用XmlDocument类,您可以循环通过xml文件的节点,然后继续添加项目到dropdownlist。 示例代码:

XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("regis.xml")); XmlNodeList colorList = doc.SelectNodes("Information/Comments/Name"); foreach (XmlNode Name in colorList) { DropDownList1.Items.Add(Name.InnerText); } 

参考: http : //r4r.co.in/asp.net/01/tutorial/asp.net/How%20to%20populate%20combobox%20from%20xml%20file%20using%20c-Sharp%20in%20asp.net。 SHTML

鉴于此XML

     Item1 Item2 Item3    

我们可以通过几种方式获取数据。 这个类有两个方法,第一个循环遍历所有节点,直到它到达我们想要的数据。 第二个将使用XmlDocument.GetElementsByTagName()方法直接转到我们想要的数据。

 using System; using System.Xml; using System.Collections.Generic; public static class MyXmlParser { ///This method will loop through each node to get to the data we want. public static List GetItemsFromXmlByLoopingThroughEachNode(string Filename) { //Create a list to store all the items. List Items = new List(); //Load the document from a file. XmlDocument doc = new XmlDocument(); doc.Load(Filename); //Loop through all the nodes in the document. foreach(XmlNode RootNode in doc.ChildNodes) { if(RootNode.NodeType != XmlNodeType.XmlDeclaration) {//If the node is not the declaration node parse it. //Loop through all the child nodes of  foreach(XmlNode Node1Node in RootNode.ChildNodes) { //Read Attributes of  XmlAttributeCollection attributes = Node1Node.Attributes; XmlAttribute Attribute1 = attributes["attribute1"]; //Attribute1.Value will give you the string contained in the attribute. //Loop through all child nodes of  foreach(XmlNode Node2Node in Node1Node.ChildNodes) { //Loop through all child nodes of  foreach(XmlNode Node3Node in Node2Node.ChildNodes) { //These nodes contain the data we want so lets add it to our List. Items.Add(Node3Node.InnerText); } } } } } //Return the List of items we found. return Items; } ///This method will use GetElementsByTagName to go right to the data we want. public static List GetItemsFromXmlUsingTagNames(string Filename, string TagName) { //Create a list to store all the items. List Items = new List(); //Load the document from a file. XmlDocument doc = new XmlDocument(); doc.Load(Filename); //Get all the  nodes. XmlNodeList Node3Nodes = doc.GetElementsByTagName(TagName); //Loop through the node list to get the data we want. foreach(XmlNode Node3Node in Node3Nodes) { //These nodes contain the data we want so lets add it to our List. Items.Add(Node3Node.InnerText); } //Return the List of items we found. return Items; } } 

获得所需数据后,可以将项目添加到ComboBox

 //Get the items from the XML file. List Items = MyXmlParser.GetItemsFromXmlUsingTagNames("C:\\test.xml","node3"); //Add them to the ComboBox ComboBox1.Items.AddRange(Items.ToArray()) 

看到

的XmlDocument

XmlNodeList中

XmlNode的

XmlAttributeCollection

XmlAttribute