Solved

Readin xml node through array

Posted on 2006-11-15
11
273 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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, …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now