如何使用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