XML reader error

Discussion in 'ASP .Net' started by Aaron, Aug 24, 2003.

  1. Aaron

    Aaron Guest

    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?


    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
     
    Aaron, Aug 24, 2003
    #1
    1. Advertisements

  2. Dim objXMLDocument As New XmlDocument()

    objXMLDocument.Load(filename)

    objXMLDocument.GetElementsByTagName("genre")

    process that tag

    or use recursion to go through all your XML document
     
    MS News \(MS ILM\), Aug 24, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.