473,508 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML reader error

I asked for a script that can read info inside a specific xml tag and
someone gave me this example.

XmlReader reader = new XmlTextReader( filename );
while ( reader.Read() )
{
if ( reader.NodeType == XmlNodeType.Element )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElementString();
// do something
break;
case "last-name":
val = reader.ReadElementString();
// do something
break;
default: break;
}
}
}

This script worked fine until I tried to open an xml file with nested
tags.

for example the xml looks like this
<book>
<genre>
<action>ABC</action>
<bio>DFK</bio>
</genre>
</book>

I would like the result to be this when i select to read the <genre>
tag

<action>ABC</action>
<bio>DFK</bio>

but it gives me this error

Exception: System.Xml.XmlException: 'Element' is an invalid node type

is there anyway to avoid this?
"Aaron" <ku*****@yahoo.com> wrote in message
news:b4**************************@posting.google.c om...
I need to make a script that reads the info inside a specific tag.

for example the xml file looks like this

<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>

and say i seleted the <first-name> tag I want the output to be
Herman

THe closest example i could find is this, but it reads everything in
the xml. i want to read only the tags i choose.
http://samples.gotdotnet.com/quickst...adXMLFile.aspx


You can use XmlTextReader to do this. Something like (c# code, but
it
could be any .Net language)

XmlReader reader = new XmlTextReader( filename );
while ( reader.Read() )
{
if ( reader.NodeType == XmlNodeType.Element )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElementString();
// do something
break;
case "last-name":
val = reader.ReadElementString();
// do something
break;
default: break;
}
}
}

-derek
--
This posting is provided "AS IS" with no warranties, and confers no
rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
Nov 17 '05 #1
1 1945
Dim objXMLDocument As New XmlDocument()

objXMLDocument.Load(filename)

objXMLDocument.GetElementsByTagName("genre")

process that tag

or use recursion to go through all your XML document

"Aaron" <ku*****@yahoo.com> wrote in message
news:b4**************************@posting.google.c om...
I asked for a script that can read info inside a specific xml tag and
someone gave me this example.

XmlReader reader = new XmlTextReader( filename );
while ( reader.Read() )
{
if ( reader.NodeType == XmlNodeType.Element )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElementString();
// do something
break;
case "last-name":
val = reader.ReadElementString();
// do something
break;
default: break;
}
}
}

This script worked fine until I tried to open an xml file with nested
tags.

for example the xml looks like this
<book>
<genre>
<action>ABC</action>
<bio>DFK</bio>
</genre>
</book>

I would like the result to be this when i select to read the <genre>
tag

<action>ABC</action>
<bio>DFK</bio>

but it gives me this error

Exception: System.Xml.XmlException: 'Element' is an invalid node type

is there anyway to avoid this?
"Aaron" <ku*****@yahoo.com> wrote in message
news:b4**************************@posting.google.c om...
I need to make a script that reads the info inside a specific tag.

for example the xml file looks like this

<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>

and say i seleted the <first-name> tag I want the output to be
Herman

THe closest example i could find is this, but it reads everything in
the xml. i want to read only the tags i choose.
http://samples.gotdotnet.com/quickst...adXMLFile.aspx


You can use XmlTextReader to do this. Something like (c# code, but
it
could be any .Net language)

XmlReader reader = new XmlTextReader( filename );
while ( reader.Read() )
{
if ( reader.NodeType == XmlNodeType.Element )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElementString();
// do something
break;
case "last-name":
val = reader.ReadElementString();
// do something
break;
default: break;
}
}
}

-derek
--
This posting is provided "AS IS" with no warranties, and confers no
rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

Nov 17 '05 #2

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

Similar topics

1
1431
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
8
27609
by: Stephan | last post by:
I'm fairly new to python and am working on parsing some delimited text files. I noticed that there's a nice CSV reading/writing module included in the libraries. My data files however, are odd...
2
5383
by: Aaron | last post by:
The example you give me worked until I tried to open an xml file with nested tags. for example the xml looks like this <book> <genre> <action>ABC</action> <bio>DFK</bio> </genre> </book>
1
4207
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
8
2497
by: Todd Bright | last post by:
Is there a way to get the current XmlNode from the reader while in the validation event handler? What I'd like to do is display the error message along with the name of its parent node. In my...
1
2107
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
2
5040
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As...
6
4959
by: dgleeson3 | last post by:
Hello All I have VB code (.Net 2005) reading from an SQL server 2005 database. Im getting InvalidCastException when doing reader.GetInt32(0) Im simply reading an int from a simple database. It...
0
1505
by: ashes | last post by:
Hi, i created a register form on a website. The information from the form has to go in the customer table in a database in MS Access. the information is going into the table perfectly. ...
0
7223
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
7321
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,...
1
7036
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
5624
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,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
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
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
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...

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.