【问题标题】:code to populate a combobox with values from an xml file使用 xml 文件中的值填充组合框的代码
【发布时间】:2011-05-04 04:31:04
【问题描述】:

如何使用 xml 文件中的值填充组合框。

【问题讨论】:

    标签: c# xml combobox


    【解决方案1】:

    使用 XmlDocument 类,您可以遍历 xml 文件的节点,然后继续将项目添加到下拉列表。 示例代码:

     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

    【讨论】:

    • 相同的代码是否适用于 windows 窗体应用程序
    • 不是真的...我不是赢表开发人员,但我想逻辑应该或多或少相同。循环通过文件,然后添加到组合框。做一些谷歌搜索。
    • 好的,我明白了,但我的 xml 文件有点复杂,我无法理解如何选择您发送的代码中的节点。
    • 嗯..我想我无能为力了...如果有帮助,请检查这个dotnettutorials.com/tutorials/xml/winform-filter-xml-cs.aspx
    【解决方案2】:

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

    这是一个让您入门的示例。 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

    【讨论】:

    • 我看到了这个链接的例子,但我在最后一行 datagrid.databind();并 cnt 找出错误
    • 您能否发布使用链接中的示例时遇到的错误?
    • 'System.Windows.Forms.DataGrid 不包含'DataBind' 的定义,并且找不到接受'System.Windows.Forms.DataBind' 类型的第一个参数的扩展方法'DataBind' (您是否缺少 using 指令或程序集引用?)
    【解决方案3】:

    鉴于此 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;       
        }
    }
    

    获得所需数据后,您可以将项目添加到组合框

    //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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多