Groups | Blog | Home
all groups > dotnet xml > august 2003 >

dotnet xml : XML reader error


kuya789 NO[at]SPAM yahoo.com
8/24/2003 1:55:11 AM
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.XmlException: 'Element' is an invalid node type

is there anyway to avoid this?


[quoted text, click to view]

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
Christoph Schittko [MVP]
8/24/2003 8:52:35 AM
Aaron,

[quoted text, click to view]
nested tags.

WHAT example that WHO gave you? There are quite a number of people reading
this ...

[quoted text, click to view]

Can you post code? Your question would make a lot make sense then.

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


[quoted text, click to view]

AddThis Social Bookmark Button