Readin xml node through array

Posted on 2006-11-15
Medium Priority
Last Modified: 2009-07-29
is there any possibility that we can traverse the nodes in xml through array. Im trying to traverse through each node using this format of for loop :

for i as integer = 1 to count.Length

instead of :
for each node in xmlNode

Is there any way we can do that.........?
Question by:shwaqar82
  • 5
  • 3
  • 2
  • +1
LVL 60

Expert Comment

by:Geert Bormans
ID: 17948511
Hi shwaqar82,

something like this?

var oNode=oXML.selectNodes("your XPath goes here");

After that you can loop over the nodes like this

                         for (j=0;  j<oNode.length;  j++)
                                  test = test + (oNode[j].nodeName);
                                  test = test + ("=");
                                  test = test + (oNode[j].childNodes[0].nodeValue);
                                  test = test + ("<br />");



Author Comment

ID: 17948619
i already have:

        Dim doc As New XmlDocument
        Dim hotelNodes As XmlNodeList
        hotelNodes = doc.SelectNodes("//HOTELS/HOTEL")

and i have xml like this:

LVL 60

Expert Comment

by:Geert Bormans
ID: 17948654
what do you want to achieve with your loop?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


Author Comment

ID: 17948698
i wana achieve the information in each hotel node throught :

for i as integer = 1 to node.length

not thru

for each node in xmlNode
LVL 35

Expert Comment

ID: 17949008
Gertone's solution accomplishes that

Author Comment

ID: 17949015
no its not ..the problem is still there
LVL 35

Expert Comment

ID: 17949271
what is the problem? what error are you getting?
LVL 60

Accepted Solution

Geert Bormans earned 1000 total points
ID: 17949403
well, my friend,
I gave you an iterator that counts up to node-length
for (j=0;  j<oNode.length;  j++)

that is pretty much the best I can do if you need something like this
> for i as integer = 1 to node.length

my syntax is javascript, yours is likely VB(script)
just make sure you start with 0 if you use your syntax

note that I did not give you this
> for each node in xmlNode

so I don't see what the problem is

With my question
>what do you want to achieve with your loop?
I meant,
what exactly do you want to do in the loop?
so I can give you a more sensible loop instead of listing name and nodevalue on seperate lines in a string variable

your question was a bit cryptic,
your comments in between don't help much either

so I would appreciate a clearer definition of the errors you encounter
and the problem you have

please note that selectNodes returns nodesets, not arrays
if you want the values in an array, then you have to use a loop similar to what I did
to put all the values in an array


LVL 12

Expert Comment

ID: 17949997
Dim node As XmlNode
for i as integer = 1 to hotelNodes.Count
node = hotelNodes.Item(i);

Are you looking for this type of syntax?

Author Comment

ID: 17950026
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Xml

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim doc As New XmlDocument
        Dim hotelNodes As XmlNodeList
        hotelNodes = doc.SelectNodes("//HOTELS/HOTEL")
        Dim count As Integer = hotelNodes.Count - 1

        Dim dhForm As New HtmlForm
        dhForm.ID = "form1"
        dhForm.Method = "POST"

        Dim dtable As New Table
        Dim dtab As Table
        Dim drow As TableRow
        Dim dcell As TableCell
        Dim dhRow As TableHeaderRow
        Dim dhcell As TableHeaderCell
        Dim dtcell As TableCell
        Dim lbl As Label
        Dim btn As HtmlButton

        dtable.Width = 650
        dtable.Height = 500
        dtable.ID = "Table1"
        dtable.BackColor = Drawing.Color.DarkKhaki
        dtable.BorderColor = Drawing.Color.Blue
        dtable.BorderStyle = BorderStyle.Ridge
        dtable.BorderWidth = 10

        For r As Integer = 0 To count
            drow = New TableRow
            drow.Width = 160
            drow.Height = 80
            drow.VerticalAlign = VerticalAlign.Middle
            drow.HorizontalAlign = HorizontalAlign.Center

            Dim flag As Integer = r + 2
            For i As Integer = r To flag
                If i > count Then          'Check if variable 'i' is greater than count or not
                    Exit For
                End If

                dhcell = New TableHeaderCell 'Creating Table Header Cell within Main Table
                dhcell.ID = "Cell Header" & i
                dhcell.BackColor = Drawing.Color.BurlyWood
                dhcell.BorderColor = Drawing.Color.DodgerBlue
                dhcell.BorderWidth = 2
                dhcell.VerticalAlign = VerticalAlign.Top
                dhcell.HorizontalAlign = HorizontalAlign.Center
                dhcell.Text = "Package " & i + 1

                dhRow = New TableHeaderRow 'Creating Table Header Row within Main Table
                dhRow.ID = "Row Header" & i
                dhRow.Cells.Add(dhcell)    'Adding Header cell in Header table

                dtab = New Table           'Creating Table within Main Table
                dtab.ID = "CellTable" & i
                dtab.Rows.Add(dhRow)       'Adding Header row to the Header table

                drow.ID = "Row" & r
                dcell = New TableCell      'Creating Cell in Main table
                dcell.ID = "CellDst" & i
                dcell.Controls.Add(dtab)   'Adding Table header in Main table cell

                lbl = New Label            'Creating Cell in Main table
                lbl.ID = "LabelDst" & i
                lbl.Text = hotelNodes.Item(i).SelectSingleNode("DEPARTURE").InnerText & " To " & hotelNodes.Item(i).SelectSingleNode("DESTINATION").InnerText
                dcell.Controls.Add(lbl)   'Adding label to table cell
                drow.Cells.Add(dcell)     'Adding cell to table row

                r = flag                  'Assigning the new value to variable 'r'
            dtable.Rows.Add(drow)         'Adding row to main table
        dhForm.Controls.Add(dtable)       'Adding table to html form
        Page.Controls.Add(dhForm)         'Adding form to page control

    End Sub
End Class

the problem is i wana show the node information after the page header in the new cell of the main table(dtabel)

Author Comment

ID: 17950167
well its showing me the result on a proper place but its not giving me the node information in the tabel cell

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question