Solved

Readin xml node through array

Posted on 2006-11-15
11
275 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nessus Scan 1 71
Load XML file into a SQL Server Table using SSIS 2012 9 76
Detect file exist or not 3 157
XML Document XPath with Namespaces 3 22
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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. 
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 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