Why XmlTextReader does not raise the FileNotFoundException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bbindae@hotmail.com

    Why XmlTextReader does not raise the FileNotFoundException

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

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

    Here is the source code I wrote.

    string fileName = "thisfilenevere xist.xml";
    XmlTextReader xmlReader = null;

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

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

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

    Please help!!
  • Jason Fay

    #2
    Re: Why XmlTextReader does not raise the FileNotFoundExc eption

    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.

    --
    JASON FAY

    <bbindae@hotmai l.comwrote in message
    news:f1cd0815-3eb6-42b5-9e28-f9a853fb8124@s1 9g2000prg.googl egroups.com...
    >I am trying to check whether XmlTextReader reads the xml file
    successfully or not.
    >
    MSDN says that XmlTextReader raise the FileNotFoundExc eption when it
    cannot find the file to read.
    >
    Here is the source code I wrote.
    >
    string fileName = "thisfilenevere xist.xml";
    XmlTextReader xmlReader = null;
    >
    try
    {
    xmlReader = new XmlTextReader(f ileName); // <---- this file does
    not exist.
    }
    catch(Exception e)
    {
    // so, FileNotFoundExc eption should be caught up here.
    }
    >
    But the result is not what I expected because FileNotFoundExc eption is
    never raised.
    >
    So, how can i check XmlTextReader reads the xml file successfully?
    >
    Please help!!

    Comment

    • Martin Honnen

      #3
      Re: Why XmlTextReader does not raise the FileNotFoundExc eption

      bbindae@hotmail .com wrote:
      I am trying to check whether XmlTextReader reads the xml file
      successfully or not.
      >
      MSDN says that XmlTextReader raise the FileNotFoundExc eption when it
      cannot find the file to read.
      >
      Here is the source code I wrote.
      >
      string fileName = "thisfilenevere xist.xml";
      XmlTextReader xmlReader = null;
      >
      try
      {
      xmlReader = new XmlTextReader(f ileName); // <---- this file does
      not exist.
      }
      catch(Exception e)
      {
      // so, FileNotFoundExc eption should be caught up here.
      }
      >
      But the result is not what I expected because FileNotFoundExc eption 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
      FileNotFoundExc eption on the first Read() call.

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


      --

      Martin Honnen --- MVP XML

      Comment

      Working...