XmlReader Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CGuy

    #1

    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(f ileName);
    //How do I find out whether this xml document contains a Root node named
    "ROOT_NODE" or not?
    }

    CGuy


  • Christoph Schittko [MVP]

    #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.Ele ment != xmlReader.NodeT ype ) ) {}

    // 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.ne t> wrote in message
    news:uqC5P$LTDH A.3144@tk2msftn gp13.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(f ileName);
    > //How do I find out whether this xml document contains a Root node[/color]
    named[color=blue]
    > "ROOT_NODE" or not?
    > }
    >
    > CGuy
    >
    >[/color]


    Comment

    • Christoph Schittko [MVP]

      #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.ne t> wrote in message
      news:eK7myB5TDH A.2480@tk2msftn gp13.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.Ele ment !=
      > xmlReader.NodeT ype ) ), C# compiler throws an XmlException - "root element
      > not present". How do I do this without getting this exception?
      >
      > CGuy
      > "Christoph Schittko [MVP]" <christophdotne tINVALID@austin .rr.com> wrote in
      > message news:uFBZvZNTDH A.1724@TK2MSFTN GP10.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.Ele ment != xmlReader.NodeT ype ) ) {}
      > >
      > > // 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.ne t> wrote in message
      > > news:uqC5P$LTDH A.3144@tk2msftn gp13.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(f ileName);
      > > > //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]


      Comment

      • CGuy

        #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(s tream);
        while(xmReader. Read() == true)
        {
        if(xmReader.Nod eType == XmlNodeType.Ele ment && xmReader.Name ==
        "ROOT_NODES ")
        {
        //Has root node
        break;
        }
        }
        }
        catch(XmlExcept ion)
        {
        xmWriter.WriteS tartDocument();
        xmWriter.WriteS tartElement("RO OT_NODE");
        }
        }
        finally
        {
        //Writer the Header for current session
        }
        }
        catch()
        {
        ...
        }


        Thanks again
        CGuy


        "Christoph Schittko [MVP]" <christophdotne tINVALID@austin .rr.com> wrote in
        message news:ev6MvG5TDH A.2116@TK2MSFTN GP12.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.ne t> wrote in message
        > news:eK7myB5TDH A.2480@tk2msftn gp13.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.Ele ment !=
        > > xmlReader.NodeT ype ) ), 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]" <christophdotne tINVALID@austin .rr.com> wrote[/color][/color]
        in[color=blue][color=green]
        > > message news:uFBZvZNTDH A.1724@TK2MSFTN GP10.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.Ele ment != xmlReader.NodeT ype ) ) {}
        > > >
        > > > // 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.ne t> wrote in message
        > > > news:uqC5P$LTDH A.3144@tk2msftn gp13.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(f ileName);
        > > > > //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]


        Comment

        Working...