如何使用xml文件中的值填充组合框。使用xml文件中的值填充组合框的代码
回答
您必须从文件中读取数据,然后才能使用类似dataset.ReadXML()的东西,然后使用它为您的组合框设置绑定。
下面是一个让你开始的例子。 http://www.codeproject.com/KB/cs/dropdownfromxml.aspx
更新:请注意,有两个DataGrid类。具有DataBind()方法的位于System.Web.UI.WebControls命名空间中。窗体窗体控件不具有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);
}
将相同的代码适用于Windows窗体应用程序 – anasooya 2011-05-03 05:19:47
不是真的......我不是一个赢的表单开发人员,但我想逻辑应该是或多或少相同。循环文件,然后添加到组合框。做一些谷歌搜索。 – pramodtech 2011-05-03 05:32:44
好吧,我知道了,但我的XML文件有点复杂,我无法理解如何选择节点,就像你发送的代码。 – anasooya 2011-05-03 06:25:41
鉴于这种XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1 attribute1="attrib1" attribute2="attrib2">
<node2>
<node3>Item1</node3>
<node3>Item2</node3>
<node3>Item3</node3>
</node2>
</node1>
</root>
我们可以获取数据的几种方法。这个类有两个方法,第一个会遍历所有的节点,直到它到达我们想要的数据。第二个将使用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<string> GetItemsFromXmlByLoopingThroughEachNode(string Filename)
{
//Create a list to store all the items.
List<string> Items = new List<string>();
//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 <root>
foreach(XmlNode Node1Node in RootNode.ChildNodes)
{
//Read Attributes of <node1>
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 <node1>
foreach(XmlNode Node2Node in Node1Node.ChildNodes)
{
//Loop through all child nodes of <node2>
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<string> GetItemsFromXmlUsingTagNames(string Filename, string TagName)
{
//Create a list to store all the items.
List<string> Items = new List<string>();
//Load the document from a file.
XmlDocument doc = new XmlDocument();
doc.Load(Filename);
//Get all the <node3> 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<string> Items = MyXmlParser.GetItemsFromXmlUsingTagNames("C:\\test.xml","node3");
//Add them to the ComboBox
ComboBox1.Items.AddRange(Items.ToArray())
见
- 1. 使用XML属性填充组合框?
- 2. 试图从xml文件中填充winrt xaml中的组合框
- 3. 用XML数据填充组合框
- 4. 使用JSON文件中的数据填充组合框
- 5. 从组合框1中选择的值填充组合框2
- 6. 填充组合框使用的ObjectDataProvider
- 7. 填充文本框中的值,如果选择组合框vb6
- 8. 用SQL Server列值填充组合框
- 9. 填充组合框
- 10. 填充组合框
- 11. 填充组合框
- 12. 填充组合框
- 13. 组合框填充
- 14. 填充组合框的ExtJS
- 15. 的excel组合框填充
- 16. 在VB.NET中使用INI文件填充组合框
- 17. 使用来自组合框的信息填充文本框
- 18. 如果组合框是那么数组值填充替代组合框VBA
- 19. 使用WSDL WebService填充组合框
- 20. 使用LINQ查询填充组合框
- 21. 使用线型填充组合框.NET
- 22. 使用数据填充组合框
- 23. UWP组合框不填充绑定值
- 24. 使用c填充xml文件的treeview#
- 25. 使用实体框架填充UWP中的组合框
- 26. 使用数组元素填充VBA中的组合框
- 27. 使用MVVM范例填充与c#的组合框使用MVVM范例填充组合框
- 28. 根据组合框中的选项填充组合框?
- 29. 使用另一个组合框中的选择来填充组合框
- 30. 从XML文件填充文本框
我看到这个链接的例子,但我在最后一行datagrid.databind得到错误();和cnt找出错误 – anasooya 2011-05-03 05:26:30
你可以发布你使用链接中的例子得到的错误吗? – KaeL 2011-05-03 05:48:52
'System.Windows.Forms.DataGrid不包含'DataBind'的定义,并且没有找到接受'System.Windows.Forms.DataBind'类型的第一个参数的扩展方法'DataBind'(你是否缺少using指令或装配参考?) – anasooya 2011-05-03 06:19:52