Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Kindly help me, How to While loop Exit . I indicated place,,,
C#
using (XmlReader reader = XmlReader.Create(new StringReader(recievedXML), xrs))
    {

        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {
                    case "start":
                        String time = DateTime.Now.TimeOfDay.ToString();
                        StartTimeTextBox.Text = time.Substring(0, time.Length-8);
                        break;
                     case "vol":

                        if (reader.Read())
                        {
                            volumeTextBox.Text = reader.Value;
                            csvData.Append(reader.Value + ";");
                        }
                        else
                        {
                            MessageBox.Show("Data Not Read");

/// ====================================> I want to whlie Loop exit here     <===============
                        }
                        String time1 = DateTime.Now.TimeOfDay.ToString();
                        EndTimeTextBox.Text = time1.Substring(0, time1.Length-8);
                        break;
                    case "hct":
                        if (reader.Read())
                        {
                            HctTextBox.Text = reader.Value;
                            csvData.Append(reader.Value + ";");
                        }
                        break;
                    case "joule":
                        if (reader.Read())
                        {
                            EnergyTextBox.Text = "00"+reader.Value;
                            csvData.Append("00"+reader.Value + ";");
                        }
                        break;
                    /*case "jouleend":
                        if (reader.Read())
                        {
                            jouleendTextBox.Text = reader.Value;
                            csvData.Append("jouleend," + reader.Value + "\n");
                        }
                        break;*/
                    case "complete":
                        if (reader.Read())
                        {
                            ProcedureCompletedTextBox.Text = "OK";
                            csvData.Append("OK" + ";");
                        }
                        break;
                    case "runtime":
                        if (reader.Read())
                        {
                            TotalTimeTextBox.Text = reader.Value;
                            csvData.Append(reader.Value + ";");
                        }
                        break;
                    case "pausetime":
                        if (reader.Read())
                        {
                            PauseTextBox.Text = reader.Value;
                            csvData.Append(reader.Value + ";");
                        }
                        break;
                    case "nrpause":
                        if (reader.Read())
                        {
                            NoOfPauseTextBox.Text = reader.Value;
                            csvData.Append(reader.Value + ";");
                        }
                        break;
                    case "temperature":
                        if (reader.Read())
                        {
                            temperatureTextBox.Text = "00"+reader.Value;
                            csvData.Append("00"+reader.Value + ";");
                        }
                        break;
                }
            }
        }
    }
}
catch (Exception e)
{
    String fileName = Path.GetPathRoot(Environment.SystemDirectory) + "\\MED_DATA\\exception_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".txt";
    File.WriteAllText(fileName, e.Message + "\n" + e.StackTrace + "\n" + e.Source + "\n" + e.InnerException + "\n" + e.ToString());
}



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
[no name] 4-Jul-14 10:08am    
So you use "break" elsewhere in your code without knowing what it does?
Why are you again checking if (reader.Read()) inside the case?
[no name] 4-Jul-14 15:15pm    
Not something I would recommend but you could generate an exception to break out if you are nested like this.

Thank you Tadit for formatting the code so that it's readable. OP observe.

1 solution

Normally you'd break out of it. But what I'd be inclined to do here is put the while loop in its own method, and use a return to get out of that.

An alternative would be to create a Boolean, and set it where you want to exit. Use the Boolean in an 'and' condition in the while statement.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 11:28am    
5ed. (Somebody voted 1, what a stupidity!)
—SA
Chakravarthi Elchuri 6-Jul-14 0:10am    
yes should be 5.

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