• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Readin xml node through array

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.........?
0
shwaqar82
Asked:
shwaqar82
  • 5
  • 3
  • 2
  • +1
1 Solution
 
Geert BormansCommented:
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 />");
                              }          


Cheers!

Geert
0
 
shwaqar82Author Commented:
i already have:

        Dim doc As New XmlDocument
        Dim hotelNodes As XmlNodeList
        doc.Load(Server.MapPath("special.xml"))
        hotelNodes = doc.SelectNodes("//HOTELS/HOTEL")

and i have xml like this:

<HOTELS>
  <HOTEL>
    <DEPARTURE>Toronto</DEPARTURE>
  </HOTEL>
  <HOTEL>
    <DEPARTURE>Toronto</DEPARTURE>
   </HOTEL>
  <HOTEL>
    <DEPARTURE>Toronto</DEPARTURE>
  </HOTEL>
</HOTELS>
0
 
Geert BormansCommented:
what do you want to achieve with your loop?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
shwaqar82Author Commented:
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
0
 
YZlatCommented:
Gertone's solution accomplishes that
0
 
shwaqar82Author Commented:
no its not ..the problem is still there
0
 
YZlatCommented:
what is the problem? what error are you getting?
0
 
Geert BormansCommented:
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

cheers

Geert
0
 
jkmyoungCommented:
Dim node As XmlNode
for i as integer = 1 to hotelNodes.Count
node = hotelNodes.Item(i);

Are you looking for this type of syntax?
0
 
shwaqar82Author Commented:
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
        doc.Load(Server.MapPath("special.xml"))
        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
        'Response.Write(count)

        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'
            Next
            dtable.Rows.Add(drow)         'Adding row to main table
        Next
        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)
0
 
shwaqar82Author Commented:
well its showing me the result on a proper place but its not giving me the node information in the tabel cell
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now