2011-05-03 99 views

回答

4

您必須從文件中讀取數據,然後才能使用類似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

+0

我看到這個鏈接的例子,但我在最後一行datagrid.databind得到錯誤();和cnt找出錯誤 – anasooya 2011-05-03 05:26:30

+0

你可以發佈你使用鏈接中的例子得到的錯誤嗎? – KaeL 2011-05-03 05:48:52

+0

'System.Windows.Forms.DataGrid不包含'DataBind'的定義,並且沒有找到接受'System.Windows.Forms.DataBind'類型的第一個參數的擴展方法'DataBind'(你是否缺少using指令或裝配參考?) – anasooya 2011-05-03 06:19:52

7

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

+0

將相同的代碼適用於Windows窗體應用程序 – anasooya 2011-05-03 05:19:47

+0

不是真的......我不是一個贏的表單開發人員,但我想邏輯應該是或多或少相同。循環文件,然後添加到組合框。做一些谷歌搜索。 – pramodtech 2011-05-03 05:32:44

+0

好吧,我知道了,但我的XML文件有點複雜,我無法理解如何選擇節點,就像你發送的代碼。 – anasooya 2011-05-03 06:25:41

2

鑑於這種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()) 

XmlDocument

XmlNodeList

XmlNode

XmlAttributeCollection

XmlAttribute