473,411 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 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!!
Jan 31 '08 #1
2 3054
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

<bb*****@hotmail.comwrote in message
news:f1**********************************@s19g2000 prg.googlegroups.com...
>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!!

Jan 31 '08 #2
bb*****@hotmail.com wrote:
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.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 31 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raghu | last post by:
Does XmlTextReader class in .net represent SAX implementation? If yes, are there any performance gains if I use C++ SAX implementation in msxml4.dll versus XmlTextReader in .net? Did any one try...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
5
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
1
by: Emsi | last post by:
Hello, how can I read values of child nodes with the XmlTextReader? File format: <root> <items> <item> <field1>value1</field1> <field2>value2</field2>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.