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

Help with getting XML node

Hi,

I have attempted to write code which will run a different query depending on the node "type"

Here is my code;

    Private Sub OpenXML(ByVal SourceFileName As String)

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName)
        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNodes In currentArray
            If currentNode = "Hardware" Then
                QueryHardware(SourceFileName)

            ElseIf XmlDoc.Value = "Software" Then
                QuerySoftware(SourceFileName)

            ElseIf XmlDoc.Value = "SoftwareUnchecked" Then
                QueryUnchecked(SourceFileName)
            End If
        Next

    End Sub

Please help.
0
nickmarshall
Asked:
nickmarshall
  • 4
  • 3
1 Solution
 
DjDezmondCommented:
I havn't tested this, but I saw where you had gone wrong. Consider this line:
   "For each CurrentNode in CurrentArray"
That line declares the variable 'CurrentNode' as whatever objects are in the 'CurrentArray' list. So there is no need to declare it before then. (declare the variable obviously, but you don't need to reference it to anything).

Private Sub OpenXML(ByVal SourceFileName As String)
        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName)
        Dim currentNode As XmlNode
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentArray
            Select case CurrentNode
              Case "Hardware"
                QueryHardware(SourceFileName)
              Case "Software"
                QuerySoftware(SourceFileName)
              Case "SoftwareUnchecked"
                QueryUnchecked(SourceFileName)
             End Select
        Next
    End Sub

Hope this works..

Dez
0
 
DjDezmondCommented:
Sorry, you need to add back:

        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")

... in the declaration area... so it will be:

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName)
        Dim currentNode As XmlNode
        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  <<-- Add back (I deleted by mistake before!)
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

Let me know how you get on...
Dez
0
 
DjDezmondCommented:
And this line "For Each currentNode In currentArray" should change to:

  For Each currentNode In currentNodes

Sorry for all the posts (im not having a very good day lol).
I've not done much work with XML documents, but as long as this line was already correct (from your original code):

  Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")

There should be no problems.

Dez
0
Independent Software Vendors: 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!

 
nickmarshallAuthor Commented:
Hi Dez,

I am getting overload resolution failed on the Case lines...

with "hardware", "software", "Softwareunchecked" underlined?
0
 
nickmarshallAuthor Commented:
It says,

value of type system.xml.xmlnode cannot be converted to a string
0
 
nickmarshallAuthor Commented:
I have modified it with;

Select Case currentNode.ToString

which seems to have worked now.

0
 
DjDezmondCommented:
Yea, sorry, like i said I had not tested it, if i had i'd of picked that up.

Glad its working now anyway.

Cheers,
Dez
0

Featured Post

Industry Leaders: 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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now