Solved

Readin xml node through array

Posted on 2006-11-15
11
278 Views
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.........?
0
Comment
Question by:shwaqar82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
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 />");
                              }          


Cheers!

Geert
0
 

Author Comment

by:shwaqar82
ID: 17948619
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17948654
what do you want to achieve with your loop?
0
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!

 

Author Comment

by:shwaqar82
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
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17949008
Gertone's solution accomplishes that
0
 

Author Comment

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

Expert Comment

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

Accepted Solution

by:
Geert Bormans earned 250 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

cheers

Geert
0
 
LVL 12

Expert Comment

by:jkmyoung
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?
0
 

Author Comment

by:shwaqar82
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
        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
 

Author Comment

by:shwaqar82
ID: 17950167
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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