locked
Switch Statement in while

    Question

  • Hi

    I`m using switch in while statement like this:

    while( !xml.EOF )
    {
      switch( xml.NodeType )
      {
       case XmlNodeType.Element:
        switch ( xml.LocalName )
        {
          case "User":
           user = new User( );
           xml.Read( );
           break;
    
          case "Username":
           user.Username = xml.ReadElementContentAsString( );
           break;
    
          case "Password":
           user.Password = xml.ReadElementContentAsString( );
           break;
        }	
        break;
    					
      case XmlNodeType.EndElement:
        if( xml.LocalName == "User" )
         users.Add( user );
        xml.Read( );
        if( xml.LocalName == "Device" )
         break;//I wanna exit from while here
        break;
    		
     }
    }
    
    I wanna exit from while but break exit from switch.
    Saturday, May 07, 2011 7:47 AM

Answers

  •  

    You can use a boolean variable 

    break out of a loop that contains a switch statement (C#) - Stackoverflow

    bool flag = true;
    while( flag )
    {
     switch( xml.NodeType )
     {
     case XmlNodeType.Element:
     switch ( xml.LocalName )
     {
      case "User":
      user = new User( );
      xml.Read( );
      break;
    
      case "Username":
      user.Username = xml.ReadElementContentAsString( );
      break;
    
      case "Password":
      user.Password = xml.ReadElementContentAsString( );
      break;
     }	
     break;
    					
     case XmlNodeType.EndElement:
     if( xml.LocalName == "User" )
      users.Add( user );
     xml.Read( );
     if( xml.LocalName == "Device" )
      flag = false;
     break;
    		
     }
     if( xml.EOF || !flag) break;
    }
    

    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
     
    Don't hate the hacker, hate the code

    Saturday, May 07, 2011 8:06 AM

All replies

  •  

    You can use a boolean variable 

    break out of a loop that contains a switch statement (C#) - Stackoverflow

    bool flag = true;
    while( flag )
    {
     switch( xml.NodeType )
     {
     case XmlNodeType.Element:
     switch ( xml.LocalName )
     {
      case "User":
      user = new User( );
      xml.Read( );
      break;
    
      case "Username":
      user.Username = xml.ReadElementContentAsString( );
      break;
    
      case "Password":
      user.Password = xml.ReadElementContentAsString( );
      break;
     }	
     break;
    					
     case XmlNodeType.EndElement:
     if( xml.LocalName == "User" )
      users.Add( user );
     xml.Read( );
     if( xml.LocalName == "Device" )
      flag = false;
     break;
    		
     }
     if( xml.EOF || !flag) break;
    }
    

    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
     
    Don't hate the hacker, hate the code

    Saturday, May 07, 2011 8:06 AM
  • Or you could use the classic label + goto:

    http://msdn.microsoft.com/en-us/library/13940fs2(v=VS.100).aspx

    Saturday, May 07, 2011 3:54 PM