473,666 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML reader error

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>

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.XmlE xception: 'Element' is an invalid node type

is there anyway to avoid this?
"Aaron" <ku*****@yahoo. com> wrote in message
news:b4******** *************** ***@posting.goo gle.com...
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.Ele ment )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElem entString();
// do something
break;
case "last-name":
val = reader.ReadElem entString();
// 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 11 '05 #1
2 5408
Aaron,
The example you give me worked until I tried to open an xml file with nested tags.

WHAT example that WHO gave you? There are quite a number of people reading
this ...
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.XmlE xception: 'Element' is an invalid node type

is there anyway to avoid this?
Can you post code? Your question would make a lot make sense then.

Christoph Schittko
Software Architect, .NET Mentor
MS MVP XML .NET

"Aaron" <ku*****@yahoo. com> wrote in message
news:b4******** *************** ***@posting.goo gle.com...
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.Ele ment )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElem entString();
// do something
break;
case "last-name":
val = reader.ReadElem entString();
// 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 11 '05 #2
You're not making it easy for me to help you ...

What exactly are you trying to do? Write elements tags with their content?
Or is there more you're trying to do?
There are no "first-name" or "last-name" elements in the XML document that
you posted in your first email. Could that be related to your problem? Can
you point to the line where the exception occurs? I don't get an exception
when I run the sample code against the XML sample you provided.

HTH,
Christoph Schittko
Sofware Architect, .NET Mentor
MS MVP XML .NET
"Aaron" <ku*****@yahoo. com> wrote in message
news:b4******** *************** ***@posting.goo gle.com...
This example
----------------
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.Ele ment )
{
string val;
switch ( reader.Name )
{
case "first-name":
val = reader.ReadElem entString();
// do something
break;
case "last-name":
val = reader.ReadElem entString();
// 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 11 '05 #3

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

Similar topics

1
1447
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 reader. Unfortunately i'm getting the following error on the line: reader = cmdLogin.ExecuteReader(); Error Message: - Procedure 'LoginValidation' expects parameter '@p_UserName', which was not supplied. Not sure where I have went wrong as I reckon...
8
27618
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 in that they are composed of lines with alternating formats. (Essentially the rows are a header record and a corresponding detail record on the next line. Each line type has a different number of fields.) Can the CSV module be coerced to read...
1
4227
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 of the post. I am using VS.net 2003, .Net 1.1, Win2k Server I have a simple schema and a simple XML document.
8
2518
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 XML format, there are parent nodes named differently with some of the same names for child nodes, so when I get an error message that references the 'LOGICALPOCKET' node that could be under more than one different parent node. I need to be able to...
1
2128
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 reader. Unfortunately i'm getting the following error on the line: reader = cmdLogin.ExecuteReader(); Error Message: - Procedure 'LoginValidation' expects parameter '@p_UserName', which was not supplied. Not sure where I have went wrong as I reckon...
1
1960
by: Aaron | last post by:
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 )
2
5052
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 IDataReader = GetReader() Dim chart As New LineChart()
5
7459
by: CDMAPoster | last post by:
I have changed how I get the shell path for Acrobat Reader based on code posted by John deKrafft. Does anyone see any problems with this code running on various Windows OS's? '--Begin Module Code 'Support functions and constants for reading a string from the registry 'I have modified the code posted by John deKrafft slightly '---- Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
6
4969
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 has two columns, User and Tel number. User is filled with 1,2,3 and tel number has 3 telephone numbers. The User data column is specified as (PK, int, not null) in MS SQL
0
1512
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. however, if i don't want a repeat of information and the system checks to see if the user is already in the database i get an error. it's either i get the error message "The user name you entered is already on file." all the time or if i put "Not...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 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...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, 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...
0
5664
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();...
0
4198
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...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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 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...

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.