Click here to Skip to main content
15,917,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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();

            }

        }

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
Posted

1 solution

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;
}

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:
C#
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;
      }
   }
 
Share this answer
 
v2
Comments
satishmachineni 6-Jan-12 3:23am    
great ,thanks for your answer, i will wotk on that,i am totally new to coding , can u explain me how to do 2nd and 3rd points in ur explanation
OriginalGriff 6-Jan-12 3:30am    
Which ones? There are two 2 + 3 points...
satishmachineni 6-Jan-12 3:35am    
i mean
2) Create your XDocument, and build your query from it.
3) Init your List with a new List<string>
OriginalGriff 6-Jan-12 3:49am    
2) You are already doing in your code - just move it out of the Tick event and into the Button click (or whatever)
3) Is similar to what you are doing at the moment - the foreach loop. All you have to do is remove the line loading the string into the TextBox.Text property and replace it with:
myListOfStrings.add(p.des.ToString());
Again, this should be in your Click event or whatever you are using.
OriginalGriff 6-Jan-12 3:50am    
Sorry - misread my original post numbers:
3) myListOfStrings = new List<string>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900