遍历xml并在文本框中显示值 [英] loop through xml and display the values in textbox

查看:68
本文介绍了遍历xml并在文本框中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void timer1_Tick(object sender, EventArgs e)
        {
            XDocument xd = XDocument.Load(@"D:\satish1\na.xml");
            var query = from p in xd.Descendants("item")

                        select new
                        {
                          //  name = p.Element("title").Value,
                            des = p.Element("description").Value
                        };

            foreach (var p in query)
            {


               // tbs.Text = p.name.ToString();
                title.Text = p.des.ToString();

            }

        }


我想循环遍历xml并连续在文本框中显示值,在调试模式下它显示所有值.我的计时器时间跨度为5秒钟


i want to loop thru xml and display the values in textbox continously, in debug mode it is showing all the values.my timer timespan is for 5 seconds

推荐答案

否,你不会那样做.
计时器发生的情况是,每次计时器到达指定的时间间隔时,您都会获得一个Tick事件.在您的示例中,您每隔五秒钟就会收到一个Tick事件.
您要做的是每次打勾时,都将整个文档逐行输出到同一文本框中,并立即用下一个覆盖.

您需要更改尝试处理此问题的方式.
在您的代码中(按下按钮或类似操作):
1)创建一个类级别列表< string>并将其声明为private
2)创建XDocument,并从中构建查询.
3)使用新的List< string>
来初始化您的列表 4)循环浏览查询结果,然后将每个字符串添加到列表中.
5)启动计时器.

在计时器滴答"事件中:

1)从列表的开头获取单个字符串
2)从列表中删除它,这样您就不再使用它
3)将TextBox.Text字段设置为字符串.

(您可能会发现Queue< string>可能更适合您-但我不知道您是否熟悉队列和相关的数据结构.)



尝试了它,我的代码是这样的
No, you don''t do it that way.
What happens with a Timer is that you get a Tick event each time the timer gets to the interval specified. In you example, you would get a single Tick event each five seconds.
What you are doing is every time you get a tick, you are outputting the entire document to teh same textbox, line by line, and overwriting it immediately with the next.

You need to change the way you are trying to handle this.
In your code (on a button press, or similar):
1) Create a Class level List<string> and declare it as private
2) Create your XDocument, and build your query from it.
3) Init your List with a new List<string>
4) Loop through your query results as you are and add each string to the List.
5) Start your Timer.

In the Timer Tick event:

1) Get a single string from the front of the List
2) Remove it from the List so you don''t use it again
3) Set the TextBox.Text field to the string.

(You may find that a Queue<string> may be more use to you - but I don''t know if you are familiar with queues and related data structures.)



"tried got it , my code is like this
if (_xmlData.Count == 0) //Populate your list
{
    counter = 0;
    XDocument xd = XDocument.Load(@"D:\satish1\na.xml");
    var query = from p in xd.Descendants("item")
                select new
                {
                    des = p.Element("description").Value
                };
    foreach (var p in query)
    {
        _xmlData.Add(p.des.ToString());
    }
}
if (counter < _xmlData.Count)
    tbs.Text = _xmlData[counter];
counter++;
if (counter == _xmlData.Count)
{
    counter = 0;
}


感谢您的帮助"


不,不.您需要将显示代码(在Timer Tick事件中)与显示代码分开(在其中读取XML).

假设您按下一个按钮即可开始操作:


thanks for ur help"


No, no. You need to separate the initialization code (where you read the XML) from the display code (in the Timer Tick event).

Assuming you press a button to start this going:

private List<string> _xmlData = new List<string>();
private void MyGoButton_Click(object sender, EventArgs e)
   {
   XDocument xd = XDocument.Load(@"D:\satish1\na.xml");
   var query = from p in xd.Descendants("item")
               select new
                  {
                  des = p.Element("description").Value
                  };
   _xmlData = new List<string>();
   foreach (var p in query) 
      {
      _xmlData.Add(p.des.ToString());
      }
   MyTimer.Start();
   }

private void MyTimer_tick(object sender, EventArgs e)
   {
   if (_xmlData.Count > 0)
      {
      string line = _xmlData[0];
      _xmlData.RemoveAt(0);
      tbs.Text = line;
      }
   }


这篇关于遍历xml并在文本框中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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