XML reader error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron

    #1

    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.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;
    }
    }
    }

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

    is there anyway to avoid this?


    "Aaron" <kuya789@yahoo. com> wrote in message
    news:b4828959.0 308212158.36cbe 511@posting.goo gle.com...[color=blue]
    > 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[/color]

    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.
  • MS News \(MS ILM\)

    #2
    Re: XML reader error

    Dim objXMLDocument As New XmlDocument()

    objXMLDocument. Load(filename)

    objXMLDocument. GetElementsByTa gName("genre")

    process that tag

    or use recursion to go through all your XML document



    "Aaron" <kuya789@yahoo. com> wrote in message
    news:b4828959.0 308241246.7f6ce abf@posting.goo gle.com...[color=blue]
    > 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.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;
    > }
    > }
    > }
    >
    > 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.XmlE xception: 'Element' is an invalid node type
    >
    > is there anyway to avoid this?
    >
    >
    > "Aaron" <kuya789@yahoo. com> wrote in message
    > news:b4828959.0 308212158.36cbe 511@posting.goo gle.com...[color=green]
    > > 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[/color]
    >
    > 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.[/color]


    Comment

    Working...