2
XDocument doc = XDocument.Load(@"XMLFile1.xml");
Kullanıcılar _kullanici = new Kullanıcılar();
string password = pnb2.Password; 
foreach (XElement element2 in doc.Descendants("sif"))
{
    foreach (XElement element1 in doc.Descendants("iban"))
    {
        foreach (XElement element3 in doc.Descendants("accountno"))
        {
            foreach (XElement element4 in doc.Descendants("money"))
            {               
                foreach (XElement element8 in doc.Descendants("acc"))
                {
                    string val1 = element2.Value;
                    string val2 = element1.Value;
                    string val3 = element3.Value;
                    string val4 = element4.Value;
                    string val8 = element8.Value;
                    if (val8 == "1" && val1 == "Abdullah")
                    {                                               
                        lbl1.Content = ("İban Numaranız :" + val2);
                        lbl2.Content = ("Hesap Numaranız :" + val3);
                        lbl3.Content = ("Bakiyeniz :" + val4);
                    }
                }  
            }
        }
    }
}

How can I stop this loop ? If val8="1" and val1=="Abdullah" I would like to show my data on the screen but this loop is entering an endless loop so nothing shows on the screen.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
A.Gecu
  • 93
  • 2
  • 12

3 Answers3

2

Fixed error in this way:

if (val8 == "1" && val1 == "Abdullah")
{
    lbl1.Content = ("Iban Numaraniz :" + val2);
    lbl2.Content = ("Hesap Numaraniz :" + val3);
    lbl3.Content = ("Bakiyeniz :" + val4);
    return;
}  
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
A.Gecu
  • 93
  • 2
  • 12
  • 1
    Just change the val1 chekc in your first loop, this will give you performance boost: `if(element2.Value != "Abdullah") continue;` – mybirthname Nov 16 '16 at 06:42
2

or you could try using "break" to stop the loop.

if (val8 == "1" && val1 == "Abdullah")
{                                               
     lbl1.Content = ("İban Numaranız :" + val2);
     lbl2.Content = ("Hesap Numaranız :" + val3);
     lbl3.Content = ("Bakiyeniz :" + val4);
     break; //stops the loop
}
Sonny Recio
  • 99
  • 1
  • 8
1

You could make use of a goto statement:

if (val8 == "1" && val1 == "Abdullah")
{
    lbl1.Content = ("İban Numaranız :" + val2);
    lbl2.Content = ("Hesap Numaranız :" + val3);
    lbl3.Content = ("Bakiyeniz :" + val4);
    goto Finished;
}  

The label Finished should be placed after the closing bracket of the outer most foreach (XElement element2 in doc.Descendants("sif")). Something like the following does your job:

Finished: 
    ;

You could check this at dot-net-fiddle.

Christos
  • 53,228
  • 8
  • 76
  • 108