Continue reading other XML elements in the face of exception



I have weird situation. I have code (using XmlReader):



bool t= true;
while (t)
{

try
{
// Start reading
t = reader.Read();
if (t == false)
break;

if ((reader.NodeType == XmlNodeType.Element))
{
...
}
}catch(Exception ex)
{
//Log
// If I get exception, next Read() fails
}
}


Don't ask why I have t variable-if I remember correctly I used it in order to continue reading other elements if I had exception withing Read() method - which previously I had inside while statement - instead of t as it is now.


You can see I designed it such that if I get exception on some line I want to proceed and process other elements. But as soon as I get exception - btw this is the exception I get:



: '.', hexadecimal value 0x00, is an invalid character. Line 1344, position 1.


The call to next Read() returns - false effectively terminating my reading logic. But I wanted it to continue reading other XML lines like I said. But it doesn't happen since Read() will return false after exception.


What can I do to avoid this? And ignore the exception and continue reading other lines?


No comments:

Post a Comment