How to break the nested if-loop in Xml Reader

How to break the nested if-loop in Xml Reader

gifthelper01
Contributor Contributor
250 Views
1 Reply
Message 1 of 2

How to break the nested if-loop in Xml Reader

gifthelper01
Contributor
Contributor

Hi All, I am using API to read several xml files (String file) in one time. Inside the XmlReader, I try to use the 2nd if-statement to break the reader and continue the foreach-loop to read through the next xml files. However, the following code will break the whole foreach-loop and only return to the 1st if-statement.

 

Can anyone advise how can I break the XmlReader and continue the foreach-loop to read through the rest xml files?

Thanks.

 

 

if (openFileDialog.ShowDialog() == DialogResult.OK)       //1st if-statement
{
  foreach (String file in openFileDialog.FileNames)       //1st foreach-loop
  {
       string path = Path.GetFullPath(file);
       using (var fileStream = File.OpenText(path))
       using (XmlReader reader = XmlReader.Create(fileStream, settings))
       {
          //some code
          if (......)                                    //2nd if-statement
          {break;}
          //some code
       }
    //some code
   }
}

 

 

0 Likes
Accepted solutions (1)
251 Views
1 Reply
Reply (1)
Message 2 of 2

RPTHOMAS108
Mentor
Mentor
Accepted solution

If you have an inner loop (while) associated with the XmlReader:

while (reader.Read())

or

while (await reader.ReadAsync())

 

Then within that somewhere you can use 'continue' for next item of while or 'break' to exit entire while

 C# loop - break vs. continue - Stack Overflow

 

Either way should be structured so you get to the part which closes the reader before end using.