sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
CGuy's Avatar

XmlReader Question


Question posted by: CGuy (Guest) on November 11th, 2005 10:49 PM
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


3 Answers Posted
Christoph Schittko [MVP]'s Avatar
Christoph Schittko [MVP] November 11th, 2005 10:49 PM
Guest - n/a Posts
#2: Re: XmlReader Question

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" <cguy@csharp.net> wrote in message
news:uqC5P$LTDHA.3144@tk2msftngp13.phx.gbl...[color=blue]
> Hi,
>
> I am using an XmlTextReader to read an xml file. It may happen that[/color]
the[color=blue]
> file is present in the disk, but it may be empty (0 bytes). I would like[/color]
to[color=blue]
> find out whether the xml file contains a valid root node or not. How do I[/color]
do[color=blue]
> 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[/color]
named[color=blue]
> "ROOT_NODE" or not?
> }
>
> CGuy
>
>[/color]


Christoph Schittko [MVP]'s Avatar
Christoph Schittko [MVP] November 11th, 2005 10:50 PM
Guest - n/a Posts
#3: Re: XmlReader Question

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" <cguy@csharp.net> wrote in message
news:eK7myB5TDHA.2480@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
with[color=blue]
> 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]" <christophdotnetINVALID@austin.rr.com> wrote in
> message news:uFBZvZNTDHA.1724@TK2MSFTNGP10.phx.gbl...[color=green]
> > You can loop with the reader through the document until you find the[/color][/color]
first[color=blue][color=green]
> > element node and then check if node's name is not ROOT_NODE. The code[/color][/color]
goes[color=blue][color=green]
> > 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" <cguy@csharp.net> wrote in message
> > news:uqC5P$LTDHA.3144@tk2msftngp13.phx.gbl...[color=darkred]
> > > Hi,
> > >
> > > I am using an XmlTextReader to read an xml file. It may happen[/color][/color][/color]
that[color=blue][color=green]
> > the[color=darkred]
> > > file is present in the disk, but it may be empty (0 bytes). I would[/color][/color][/color]
like[color=blue][color=green]
> > to[color=darkred]
> > > find out whether the xml file contains a valid root node or not. How[/color][/color][/color]
do[color=blue]
> I[color=green]
> > do[color=darkred]
> > > 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[/color]
> > named[color=darkred]
> > > "ROOT_NODE" or not?
> > > }
> > >
> > > CGuy
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


CGuy's Avatar
Guest - n/a Posts
#4: Re: XmlReader Question

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]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:ev6MvG5TDHA.2116@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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" <cguy@csharp.net> wrote in message
> news:eK7myB5TDHA.2480@tk2msftngp13.phx.gbl...[color=green]
> > 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[/color]
> with[color=green]
> > 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[/color][/color]
element[color=blue][color=green]
> > not present". How do I do this without getting this exception?
> >
> > CGuy
> > "Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote[/color][/color]
in[color=blue][color=green]
> > message news:uFBZvZNTDHA.1724@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > You can loop with the reader through the document until you find the[/color][/color]
> first[color=green][color=darkred]
> > > element node and then check if node's name is not ROOT_NODE. The code[/color][/color]
> goes[color=green][color=darkred]
> > > 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" <cguy@csharp.net> wrote in message
> > > news:uqC5P$LTDHA.3144@tk2msftngp13.phx.gbl...
> > > > Hi,
> > > >
> > > > I am using an XmlTextReader to read an xml file. It may happen[/color][/color]
> that[color=green][color=darkred]
> > > the
> > > > file is present in the disk, but it may be empty (0 bytes). I would[/color][/color]
> like[color=green][color=darkred]
> > > to
> > > > find out whether the xml file contains a valid root node or not. How[/color][/color]
> do[color=green]
> > I[color=darkred]
> > > 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[/color][/color][/color]
node[color=blue][color=green][color=darkred]
> > > named
> > > > "ROOT_NODE" or not?
> > > > }
> > > >
> > > > CGuy
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


 
Not the answer you were looking for? Post your question . . .
196,808 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,808 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors