Why XmlTextReader does not raise the FileNotFoundException

B

bbindae

I am trying to check whether XmlTextReader reads the xml file
successfully or not.

MSDN says that XmlTextReader raise the FileNotFoundException when it
cannot find the file to read.

Here is the source code I wrote.

string fileName = "thisfileneverexist.xml";
XmlTextReader xmlReader = null;

try
{
xmlReader = new XmlTextReader(fileName); // <---- this file does
not exist.
}
catch(Exception e)
{
// so, FileNotFoundException should be caught up here.
}

But the result is not what I expected because FileNotFoundException is
never raised.

So, how can i check XmlTextReader reads the xml file successfully?

Please help!!
 
J

Jason Fay

You need to read the file before the XMLTextReader tries to access it.
Right now, you're simply telling it where the file is, but not trying to
access it.

add xmlReader.read into your try / catch, and you will see the exception.
 
M

Martin Honnen

I am trying to check whether XmlTextReader reads the xml file
successfully or not.

MSDN says that XmlTextReader raise the FileNotFoundException when it
cannot find the file to read.

Here is the source code I wrote.

string fileName = "thisfileneverexist.xml";
XmlTextReader xmlReader = null;

try
{
xmlReader = new XmlTextReader(fileName); // <---- this file does
not exist.
}
catch(Exception e)
{
// so, FileNotFoundException should be caught up here.
}

But the result is not what I expected because FileNotFoundException is
never raised.

So, how can i check XmlTextReader reads the xml file successfully?

If the file does not exist then XmlTextReader will throw the
FileNotFoundException on the first Read() call.

However with .NET 2.0 and later the preferred way to create an XmlReader
is XmlReader.Create("file.xml") and I think that throws on the Create
call without requiring a Read() call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top