473,473 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XmlReader Question

Hi,

I am using an XmlTextReader to read an xml file. It may happen that the
file is present in the disk, but it may be empty (0 bytes). I would like to
find out whether the xml file contains a valid root node or not. How do I do
this?

This is what I need
if(File.Exists(fileName))
{
XmlTextReader xmReader = new XmlTextReader(fileName);
//How do I find out whether this xml document contains a Root node named
"ROOT_NODE" or not?
}

CGuy
Nov 13 '05 #1
3 7893
You can loop with the reader through the document until you find the first
element node and then check if node's name is not ROOT_NODE. The code goes
something like this:

while( ( true == xmlReader.Read() )
&& ( XmlNodeType.Element != xmlReader.NodeType ) ) {}

// now we're at the first element node and can check the name:
if( "ROOT_NODE" == xmlReader.Name )
{
// it is ...
}
else
{
// it isn't
}

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"CGuy" <cg**@csharp.net> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
Hi,

I am using an XmlTextReader to read an xml file. It may happen that the file is present in the disk, but it may be empty (0 bytes). I would like to find out whether the xml file contains a valid root node or not. How do I do this?

This is what I need
if(File.Exists(fileName))
{
XmlTextReader xmReader = new XmlTextReader(fileName);
//How do I find out whether this xml document contains a Root node named "ROOT_NODE" or not?
}

CGuy

Nov 13 '05 #2
I don't know about any way to avoid the exception, but could you help me
understand what's bad about the exception? Does the exception not contain
enough information for you? Is "no root element" the same state as "empty
file" or do you need to treat them separately? You can easily do both by
handling the exception correctly, but I'd like to understand before I post
more samples.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"CGuy" <cg**@csharp.net> wrote in message
news:eK**************@tk2msftngp13.phx.gbl...
Hi Christoph,

Thanks for your comments. But this doesn't solve my problem - it may
happen that the XML file I'm processing may exist in the harddrive, but with no data (0 bytes - due to some error in the previous run). So, when I do
while( ( true == xmlReader.Read() ) && ( XmlNodeType.Element !=
xmlReader.NodeType ) ), C# compiler throws an XmlException - "root element
not present". How do I do this without getting this exception?

CGuy
"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:uF**************@TK2MSFTNGP10.phx.gbl...
You can loop with the reader through the document until you find the first element node and then check if node's name is not ROOT_NODE. The code goes something like this:

while( ( true == xmlReader.Read() )
&& ( XmlNodeType.Element != xmlReader.NodeType ) ) {}

// now we're at the first element node and can check the name:
if( "ROOT_NODE" == xmlReader.Name )
{
// it is ...
}
else
{
// it isn't
}

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"CGuy" <cg**@csharp.net> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
Hi,

I am using an XmlTextReader to read an xml file. It may happen
that the
file is present in the disk, but it may be empty (0 bytes). I would
like to
find out whether the xml file contains a valid root node or not. How
do I
do
this?

This is what I need
if(File.Exists(fileName))
{
XmlTextReader xmReader = new XmlTextReader(fileName);
//How do I find out whether this xml document contains a Root node

named
"ROOT_NODE" or not?
}

CGuy



Nov 15 '05 #3
Hi Christoph,

I wanted to avoid the exception because I wanted to avoid nested
try-catch blocks (In the code, I have 2 outer levels tray-catch blocks).
Anyway, I have followed your approach and everything works fine. This is how
I have done it

try
{
//Initialize XmlTextWriter
try
{
XmlTextReader xmReader = new XmlTextReader(stream);
while(xmReader.Read() == true)
{
if(xmReader.NodeType == XmlNodeType.Element && xmReader.Name ==
"ROOT_NODES")
{
//Has root node
break;
}
}
}
catch(XmlException)
{
xmWriter.WriteStartDocument();
xmWriter.WriteStartElement("ROOT_NODE");
}
}
finally
{
//Writer the Header for current session
}
}
catch()
{
...
}
Thanks again
CGuy
"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:ev**************@TK2MSFTNGP12.phx.gbl...
I don't know about any way to avoid the exception, but could you help me
understand what's bad about the exception? Does the exception not contain
enough information for you? Is "no root element" the same state as "empty
file" or do you need to treat them separately? You can easily do both by
handling the exception correctly, but I'd like to understand before I post
more samples.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"CGuy" <cg**@csharp.net> wrote in message
news:eK**************@tk2msftngp13.phx.gbl...
Hi Christoph,

Thanks for your comments. But this doesn't solve my problem - it may
happen that the XML file I'm processing may exist in the harddrive, but

with
no data (0 bytes - due to some error in the previous run). So, when I do
while( ( true == xmlReader.Read() ) && ( XmlNodeType.Element !=
xmlReader.NodeType ) ), C# compiler throws an XmlException - "root element
not present". How do I do this without getting this exception?

CGuy
"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in message news:uF**************@TK2MSFTNGP10.phx.gbl...
You can loop with the reader through the document until you find the

first element node and then check if node's name is not ROOT_NODE. The code goes something like this:

while( ( true == xmlReader.Read() )
&& ( XmlNodeType.Element != xmlReader.NodeType ) ) {}

// now we're at the first element node and can check the name:
if( "ROOT_NODE" == xmlReader.Name )
{
// it is ...
}
else
{
// it isn't
}

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"CGuy" <cg**@csharp.net> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
> Hi,
>
> I am using an XmlTextReader to read an xml file. It may happen that the
> file is present in the disk, but it may be empty (0 bytes). I would like to
> find out whether the xml file contains a valid root node or not. How

do
I
do
> this?
>
> This is what I need
> if(File.Exists(fileName))
> {
> XmlTextReader xmReader = new XmlTextReader(fileName);
> //How do I find out whether this xml document contains a Root node named
> "ROOT_NODE" or not?
> }
>
> CGuy
>
>



Nov 15 '05 #4

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

Similar topics

3
by: CGuy | last post by:
Hi, I am using an XmlTextReader to read an xml file. It may happen that the file is present in the disk, but it may be empty (0 bytes). I would like to find out whether the xml file contains a...
5
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: ...
2
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and...
11
by: Ben | last post by:
Hi, I'm just moving to VB.NET and I'm trying to load a recordset intoan XMLReader and loop through the records. When I use the.ReadElementString() method to get the next 'record' if it hasreached...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
2
by: Jeff Calico | last post by:
Hello all. I am implementing a SAX filter to strip a bunch of unneeded elements out of a large XML file. I found a book "Java & XML" by Brett McLaughlin, and an interesting article by him wich...
2
by: jonfroehlich | last post by:
According to the MSDN documentation within the XmlTextReader class for ..NET 2.0, the recommended practice to create XmlReader instances is using the XmlReaderSettings class and the...
5
by: heday60 | last post by:
I've got this application that watches a directory for XML's and then parses them into a DB. It then moves them based on if the XML was succesful or a bad XML... foreach(FileInfo file in...
1
by: dignan.tenenbaum | last post by:
Hello, I'm using the XmlReader.ReadOuterXml() method to return the string representation of an xml node. The XmlReader is created with a file path and a XmlReaderSettings object. This was...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.