XmlNodeReader

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

    XmlNodeReader

    All

    I have an XML document as illustrated in the extract below. I am trying to
    use the XmlNodeReader to extract information from the document. What I am
    having trouble with is when I read the line of the document I am unable to
    read the value of the element. Below is an extract of the source code that I
    am using to read the other parts of the document that I want to extract. Any
    assistance would be appreciated.

    Thanks

    ie. What I am trying to extract

    <value code="Mr">Mr</value> - want to extract "value"
    <value code="Mrs">Mrs</value> - want to extract "value"
    .....etc
    <value code="NSW">NSW</value> - want to extract "value"
    <caption>State: </caption> - want to extract "caption"


    --- XML Doc ---

    <root>
    <selection attr="1">
    <value code="Mr">Mr</value>
    <value code="Mrs">Mrs</value>
    <value code="Ms">Ms</value>
    <value code="Miss">Mis s</value>
    <caption>Title: </caption>
    </selection>
    <selection attr="2">
    <value code="QLD">QLD</value>
    <value code="VIC">VIC</value>
    <value code="NSW">NSW</value>
    <caption>State: </caption>
    </selection>
    </root>

    --- End XML Doc ---

    --- Code ---

    Dim sr As New System.IO.Strea mReader("c:\xml .xml")
    Dim sw As New System.IO.Strea mWriter("c:\out put.txt")
    Dim attr As String
    Dim doc As New Xml.XmlDocument

    doc.Load(sr)
    Dim reader As New Xml.XmlNodeRead er(doc)

    While reader.Read()
    Select Case reader.NodeType
    Case Xml.XmlNodeType .Element
    If reader.Name = "selection" Then
    attr = reader.GetAttri bute("attr")
    End If
    Case Xml.XmlNodeType .Text
    sw.WriteLine(at tr & ", " & reader.Value)
    End Select
    End While

    sw.Close()

    --- End Code ---
  • Derek Harmon

    #2
    Re: XmlNodeReader

    "David" <David@discussi ons.microsoft.c om> wrote in message news:46E5F750-7F7F-4C34-BCE1-C118C9D82074@mi crosoft.com...[color=blue]
    > I have an XML document as illustrated in the extract below. I am trying to
    > use the XmlNodeReader to extract information from the document. What I am
    > having trouble with is when I read the line of the document I am unable to
    > read the value of the element.[/color]

    Those are called an element's "local name" (you can only
    read the Element's LocalName when the XmlNodeReader
    is currently positioned on it, so if you need it later you must
    store it in a local variable).

    Elements have no "values", but they may have "children".

    Once you learn these names the API will become a lot easier. ;-)
    [color=blue]
    > <caption>State: </caption> - want to extract "caption"[/color]
    : :
    Dim lastElementName As String = String.Empty[color=blue]
    > While reader.Read()
    > Select Case reader.NodeType
    > Case Xml.XmlNodeType .Element
    > If reader.Name = "selection" Then
    > attr = reader.GetAttri bute("attr")
    > End If[/color]
    lastElementName = reader.LocalNam e[color=blue]
    > Case Xml.XmlNodeType .Text[/color]
    sw.WriteLine( attr & ", " & lastElementName )[color=blue]
    > End Select
    > End While[/color]


    Derek Harmon


    Comment

    • David

      #3
      Re: XmlNodeReader

      Derek

      Thanks for your responce.

      I have tried adding a Message Box to display the local name when I read the
      TextNode as illistrated below, and the Message Box is blank. Am I doing
      something wrong.

      Thanks

      Case Xml.XmlNodeType .Text
      MsgBox(reader.L ocalName)
      sw.WriteLine( attr & ", " & lastElementName )


      "Derek Harmon" wrote:
      [color=blue]
      > "David" <David@discussi ons.microsoft.c om> wrote in message news:46E5F750-7F7F-4C34-BCE1-C118C9D82074@mi crosoft.com...[color=green]
      > > I have an XML document as illustrated in the extract below. I am trying to
      > > use the XmlNodeReader to extract information from the document. What I am
      > > having trouble with is when I read the line of the document I am unable to
      > > read the value of the element.[/color]
      >
      > Those are called an element's "local name" (you can only
      > read the Element's LocalName when the XmlNodeReader
      > is currently positioned on it, so if you need it later you must
      > store it in a local variable).
      >
      > Elements have no "values", but they may have "children".
      >
      > Once you learn these names the API will become a lot easier. ;-)
      >[color=green]
      > > <caption>State: </caption> - want to extract "caption"[/color]
      > : :
      > Dim lastElementName As String = String.Empty[color=green]
      > > While reader.Read()
      > > Select Case reader.NodeType
      > > Case Xml.XmlNodeType .Element
      > > If reader.Name = "selection" Then
      > > attr = reader.GetAttri bute("attr")
      > > End If[/color]
      > lastElementName = reader.LocalNam e[color=green]
      > > Case Xml.XmlNodeType .Text[/color]
      > sw.WriteLine( attr & ", " & lastElementName )[color=green]
      > > End Select
      > > End While[/color]
      >
      >
      > Derek Harmon
      >
      >
      >[/color]

      Comment

      • Nak

        #4
        Re: XmlNodeReader

        Hi David,

        The following is some untested code that should get you going. If you
        get desparate I'll knock up a working example for you, but I think you
        should be able to see what's happening here. The only tricky bit to
        understand might be the XPath expressions which can be read about at the
        following address

        Sorry! We can't seem to find the resource you're looking for


        -----------

        DIm doc as new xmldocument
        call doc.load("filen ame")

        Dim nodelist As XmlNodeList = doc.SelectNodes ("root/selection[@attr='1']")

        Dim i as integer
        for i = 0 to nodelist.count -1

        Dim nodelist2 as XmlNodeList = nodelist.item(i ).SelectNodes(" value")

        Dim y as integer
        for y = 0 to nodelist2.count - 1

        nodelist2.item( y).atttributes( "code").val ue

        nodelist2.item( y).innertext


        next

        next

        -----------

        Nick.

        <root>

        Select this section using - root/selection[@attr='1']
        <selection attr="1">

        To select these in 1 I *think* you can do -
        root/selection[@attr='1']/value
        <value code="Mr">Mr</value>
        <value code="Mrs">Mrs</value>
        <value code="Ms">Ms</value>
        <value code="Miss">Mis s</value>

        <caption>Title: </caption>
        </selection>

        Select this section using - root/selection[@attr='2']
        <selection attr="2">

        To select these in 1 I *think* you can do -
        root/selection[@attr='2']/value
        <value code="QLD">QLD</value>
        <value code="VIC">VIC</value>
        <value code="NSW">NSW</value>
        <caption>State: </caption>
        </selection>

        </root>


        Comment

        • Nak

          #5
          Re: XmlNodeReader

          Hi David,

          Sorry, didn't mention what the following lines were for

          Obtain an attribute[color=blue]
          > nodelist2.item( y).atttributes( "code").val ue[/color]

          Obtain the inner text[color=blue]
          > nodelist2.item( y).innertext[/color]

          <element attribute="valu e">InnerText </element>

          Nick.

          "Nak" <a@a.com> wrote in message
          news:Onl5uM$8EH A.1300@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Hi David,
          >
          > The following is some untested code that should get you going. If you
          > get desparate I'll knock up a working example for you, but I think you
          > should be able to see what's happening here. The only tricky bit to
          > understand might be the XPath expressions which can be read about at the
          > following address
          >
          > http://www.w3schools.com/xpath/xpath_syntax.asp
          >
          > -----------
          >
          > DIm doc as new xmldocument
          > call doc.load("filen ame")
          >
          > Dim nodelist As XmlNodeList = doc.SelectNodes ("root/selection[@attr='1']")
          >
          > Dim i as integer
          > for i = 0 to nodelist.count -1
          >
          > Dim nodelist2 as XmlNodeList = nodelist.item(i ).SelectNodes(" value")
          >
          > Dim y as integer
          > for y = 0 to nodelist2.count - 1
          >
          > nodelist2.item( y).atttributes( "code").val ue
          >
          > nodelist2.item( y).innertext
          >
          >
          > next
          >
          > next
          >
          > -----------
          >
          > Nick.
          >
          > <root>
          >
          > Select this section using - root/selection[@attr='1']
          > <selection attr="1">
          >
          > To select these in 1 I *think* you can do -
          > root/selection[@attr='1']/value
          > <value code="Mr">Mr</value>
          > <value code="Mrs">Mrs</value>
          > <value code="Ms">Ms</value>
          > <value code="Miss">Mis s</value>
          >
          > <caption>Title: </caption>
          > </selection>
          >
          > Select this section using - root/selection[@attr='2']
          > <selection attr="2">
          >
          > To select these in 1 I *think* you can do -
          > root/selection[@attr='2']/value
          > <value code="QLD">QLD</value>
          > <value code="VIC">VIC</value>
          > <value code="NSW">NSW</value>
          > <caption>State: </caption>
          > </selection>
          >
          > </root>
          >[/color]


          Comment

          Working...