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 3 7908
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" <cg**@csharp.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.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(f ileName); //How do I find out whether this xml document contains a Root node
named "ROOT_NODE" or not? }
CGuy
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.ne t> wrote in message
news:eK******** ******@tk2msftn gp13.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.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]" <ch************ ********@austin .rr.com> wrote in message news:uF******** ******@TK2MSFTN GP10.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.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" <cg**@csharp.ne t> wrote in message news:uq******** ******@tk2msftn gp13.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(f ileName); //How do I find out whether this xml document contains a Root node named "ROOT_NODE" or not? }
CGuy
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]" <ch************ ********@austin .rr.com> wrote in
message news:ev******** ******@TK2MSFTN GP12.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.ne t> wrote in message news:eK******** ******@tk2msftn gp13.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.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]" <ch************ ********@austin .rr.com> wrote
in message news:uF******** ******@TK2MSFTN GP10.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.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" <cg**@csharp.ne t> wrote in message news:uq******** ******@tk2msftn gp13.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(f ileName); > //How do I find out whether this xml document contains a Root
node named > "ROOT_NODE" or not? > } > > CGuy > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 valid root node or not. How do I do
this?
This is what I need
if(File.Exists(fileName))
{
|
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:
XmlReader instance from another module (beyond my control)
|
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 public inside the class)
|
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 the last record (eof) it fails with "Invalid attempt toread when reader is closed". It doesn't seem capable of findingthis out until the error has occured though (Readstate isinteractive prior to the failure and EOF is false.)
Is this limited...
|
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:
Public Class Form1
Inherits System.Windows.Forms.Form
| |
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, reader2, after creating new XmlReaderSettings and a new
XmlParserContext.
// customContext and customSettings may or may not make use
reader1's properties
XmlParserContext customContext = new XmlParserContext(...);
|
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 address my issues:
http://www-128.ibm.com/developerworks/xml/library/x-tipbigdoc3.html
However, doing it in that specified way does not seem to work at all!
Doing it very differently *seems* to work, but most likely I am not
understanding...
|
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 XmlReader.Create() method.
However, the problem is, the XmlReader class does not expose certain
properties that I need, e.g., LineNumber, LinePosition, etc. I would
like to follow Microsoft's recommended practices, but I'm not sure how
I can get XmlTextReader...
|
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 dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
|
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 working fine until a
default namespace was introduced. When the default namespace is
defined the ReadOuterXml() method began adding a bunch of extra
namespace declarations to the string it returned.
xml example ...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |