I am reading a very large XML file, which I have to read as a stream, like so:
public IEnumerable<something> GetStuff()
{
foreach(var gzipStream in GetGZips())
{
using (var reader = XmlReader.Create(gzipStream, new XmlReaderSettings{ CheckCharacters = false }))
{
reader.MoveToContent();
while (reader.Read()) //<-- Exception here
{
//Do stuff
yield return something;
}
}
}
}
I get an invalid char exception, part-way through the processing:
' ', hexadecimal value 0x19, is an invalid character. Line 655, position 45.
Given you're not allowed to yield return inside a try-catch - what is a nice way to simply abort the processing of the current Xml doc (and completing the Enumeration), in the case of an error?
try/finally is no good - as the exception breaks the processing of the whole IEnumerable.
I'm not able to perform any pre-processing on the files.
If you really can't do any preprocessing, and absolutely must generate the enumeration while parsing the XML, what about if you replace your while loop with:
bool IsMoreXml = true;
while (IsMoreXml)
{
var ValuesRead = null; //not sure what you're reading
try
{
IsMoreXml = reader.Read();
if(!IsMoreXml) break;
//Do Stuff
ValuesRead = whateverwereadfromxml;
}
catch (XmlException ex)
{
//do what you gotta do
break;
}
if(ValuesRead != null)
yield return ValuesRead;
}
There are other possible exceptions you should be handling though, but not sure if you were handling those where it's being called from. It's not elegant, but then I'm not sure what your limitations are (for ex, no preprocessing)