如何从列表框中的节点读取值 [英] How to read values from a node which is in list box

查看:88
本文介绍了如何从列表框中的节点读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListBox listBox1 = new ListBox();
string fileName = (Server.MapPath("OrgList.xml"));
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();
// Compile a standard XPath expression
XPathExpression expr;
expr = nav.Compile("/main/DATA_RECORD");
XPathNodeIterator iterator = nav.Select(expr);
// Iterate on the node set
listBox1.Items.Clear();
try
{
   while (iterator.MoveNext())
   {
      XPathNavigator nav2 = iterator.Current.Clone();
      listBox1.Items.Add(nav2.Value);
   }
}
catch (Exception ex)
{
   Response.Write(ex.ToString());
}


在程序数据记录中是其中包含3个项(名称,度数和ID)的节点. iteraor与节点一起传递,并循环遍历所有节点以及添加到列表框中的所有节点.现在,我想分别访问这3个项目.


In the program data record is the node which has 3 items(name,degree,and ID) in it. The iteraor gets passed with the node and it loops through all the node and all the nodes added to a list box. Now I want to access the 3 items separately. What is the syntax for it?

推荐答案

简单示例显示直接通过索引和foreach;

Simple Example showing direct via index and foreach;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        
        //Add some items to a ListBox on the form
        for (int x = 0; x < 5; x++)
        {
            listBox1.Items.Add("Item " + x.ToString());
        }

    }

    //Access an item by Index in collection
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(listBox1.Items[2].ToString());
    }

    //Iterate the collection
    private void button2_Click(object sender, EventArgs e)
    {
        foreach (String item in listBox1.Items)
        {
            MessageBox.Show(item);
        }
    }
}


这篇关于如何从列表框中的节点读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆