Solved

XML select node, then select case not working

Posted on 2006-10-20
27
223 Views
Last Modified: 2010-04-23
Experts,

I am trying to run a sepcific sub based on a node type within a chosen XML file.  I have a select case that runs a specific subroutine based on the node string.

Here is my code;
   
 Private Sub OpenXML(ByVal SourceFileName As String) ' Open XML document and run query based on XML node <Type></Type>

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml
        Dim currentNode As XmlNode

        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentNodes  '<--- Here I run a Select Case based on the string within the node, ie.  "Hardware", "Checked" and "Unchecked"
            Select Case currentNode.ToString
                Case "Hardware"
                    QHardware(SourceFileName)
                    MessageBox.Show("Hardware")
                Case "Checked"
                    QChecked(SourceFileName)
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    QUnchecked(SourceFileName)
                    MessageBox.Show("Unchecked")
            End Select
        Next

None of the select case statements are giving me output to a messagebox, so I dont think it is working.

I have also checked the Type node for all three xml files, and they all read correctly.
0
Comment
Question by:nickmarshall
  • 12
  • 6
  • 4
  • +2
27 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17773619
Hi try putting a:

 MessageBox.Show(currentNode.ToString())

statement just before the select case is entered, this will allow you to verify if the node name is correct. I suspect that you have to use the property Name to get the nodes name instead of the to string, e.g:

Select Case currentNode.Name
...

If this still doesnt work can you post the XML file so that I can see if your selecting the node correctly?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17773758
try InnerText:

 Private Sub OpenXML(ByVal SourceFileName As String) ' Open XML document and run query based on XML node <Type></Type>

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml
        Dim currentNode As XmlNode

        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentNodes  '<--- Here I run a Select Case based on the string within the node, ie.  "Hardware", "Checked" and "Unchecked"
            Select Case currentNode.InnerText
                Case "Hardware"
                    QHardware(SourceFileName)
                    MessageBox.Show("Hardware")
                Case "Checked"
                    QChecked(SourceFileName)
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    QUnchecked(SourceFileName)
                    MessageBox.Show("Unchecked")
            End Select
        Next
    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17773761
or

 Private Sub OpenXML(ByVal SourceFileName As String) ' Open XML document and run query based on XML node <Type></Type>

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml
        Dim currentNode As XmlNode

        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentNodes  '<--- Here I run a Select Case based on the string within the node, ie.  "Hardware", "Checked" and "Unchecked"
            Select Case Trim(currentNode.InnerText)
                Case "Hardware"
                    QHardware(SourceFileName)
                    MessageBox.Show("Hardware")
                Case "Checked"
                    QChecked(SourceFileName)
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    QUnchecked(SourceFileName)
                    MessageBox.Show("Unchecked")
            End Select
        Next
    End Sub
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17773837
nickmarshall, this is the third question you've raised on this same sub-routine. If it doesn't work first time, you dont need to close the question, we will keep working at it until it does exactly what you need. No need to waste the points like that.

And not to mention I feel bad for it not working from the first question!

Dez
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17773917
Hi Dez,

Sorry about that, but I didnt get chance to test, and everything looked correct to me, so I though it was complete.

I have attempted to use;

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml
        Dim currentNode As XmlNode

        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentNodes  '<--- Here I run a Select Case based on the string within the node, ie.  "Hardware", "Checked" and "Unchecked"
            Select Case Trim(currentNode.InnerText)
                Case "Hardware"
                    QHardware(SourceFileName)
                    MessageBox.Show("Hardware")
                Case "Checked"
                    QChecked(SourceFileName)
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    QUnchecked(SourceFileName)
                    MessageBox.Show("Unchecked")
            End Select
        Next

However I dont get any output from messagebox now.

0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17773934
There is only one "Type" tag within every XML document I am searching;

The Type tag has either, "hardware", "Checked" or "Unchecked" in it.

I need to pull this out as a string and then store it within a variable which will be used to run a specifc sub (the sub will query the database)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17773989
"hardware" or "Hardware"?

let me see your XML
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774039
<?xml version="1.0" ?>
- <Hardware>
- <Hardware ISBN="1861005652" Title="Hardware list">
  <Type>Hardware</Type>
- <CPU>
  <CurrentClockSpeed>1716</CurrentClockSpeed>
  <Manufacturer>GenuineIntel</Manufacturer>
  <Name>Intel(R) Celeron(R) CPU 1.70GHz</Name>
  <SocketDesignation>Socket 478</SocketDesignation>
  <Status>OK</Status>
  </CPU>
- <System>
  <CSName>SUPPORT</CSName>
  <Name>Microsoft Windows XP Professional|F:\WINDOWS|\Device\Harddisk0\Partition2</Name>
  <Organization>MyOrg</Organization>
  <RegisteredUser>The Hostmaster</RegisteredUser>
  <SerialNumber>55274-OEM-0014286-7777</SerialNumber>
  <TotalVisibleMemorySize>1048052</TotalVisibleMemorySize>
  <WindowsDirectory>F:\WINDOWS</WindowsDirectory>
  </System>
- <Sound>
  <Manufacturer>Intel</Manufacturer>
  <ProductName>Intel(r) 82801BA/BAM AC'97 Audio Controller</ProductName>
  </Sound>
- <Motherboard>
  <Manufacturer>Gigabyte Technology Co., Ltd.</Manufacturer>
  <Product>8ID533</Product>
  </Motherboard>
- <DisplayAdapter>
  <DriverVersion>6.13.10.4109</DriverVersion>
  <Name>NVIDIA Quadro4 NVS</Name>
  </DisplayAdapter>
- <DisplayAdapter>
  <DriverVersion>4.00.1636.1</DriverVersion>
  <Name>Winvnc video hook driver</Name>
  </DisplayAdapter>
- <CdromDrive>
  <Drive>D:</Drive>
  <Name>CD-RW CDR-7S52</Name>
  </CdromDrive>
- <LogicalDrives>
  <DeviceID>A:</DeviceID>
  </LogicalDrives>
- <LogicalDrives>
  <DeviceID>C:</DeviceID>
  <FileSystem>NTFS</FileSystem>
  <FreeSpace>487625728</FreeSpace>
  <Size>2097413632</Size>
  </LogicalDrives>
- <LogicalDrives>
  <DeviceID>D:</DeviceID>
  </LogicalDrives>
- <LogicalDrives>
  <DeviceID>F:</DeviceID>
  <FileSystem>NTFS</FileSystem>
  <FreeSpace>21185536000</FreeSpace>
  <Size>37918507008</Size>
  </LogicalDrives>
  </Hardware>
  </Hardware>
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774051
It is the <Type></Type> node that I am after.

In all the Xml files that I am checking, there will only be one type node.
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17774055
Have you put a msgbox on the select case variable (is it currentNode.InnerText?) as Zephyr__  suggested?

Especially when your pulling data from a file, i always check to see the format of the text im pulling in before i start writing the routines.

Dez
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17774069
change the following line

Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>

to

if the xml is like this

<ROOT>
              <TYPE>
              </TYPE>
              <TYPE>
              </TYPE>
</ROOT>

note the parameter to selectnodes and the documentelement property
also remember that all the xml tags are case sensitive

Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.SelectNodes("/ROOT/TYPE")  '<--- Here I am trying to specify the node, which is simply <Type></Type>


if XML is like this
              <TYPE>
              </TYPE>
              <TYPE>
              </TYPE>

Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.SelectNodes("/TYPE")  '<--- Here I am trying to specify the node, which is simply <Type></Type>

Hope this Helps :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17774103
this case you should write "/Hardware/Hardware/Type"

Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.SelectNodes("/Hardware/Hardware/Type")  '<--- Here I am trying to specify the node, which is simply <Type></Type>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17774117
change

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

to

  Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type")
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17774172
I don't understand why your using SelectNodes if there will only ever be one Type node... just use SelectSingleNode and remove the for each loop:

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml

        Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("//Hardware/Hardware/Type")
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        If (Not typeNode Is Nothing) Then
            Select Case typeNode.InnerText
                Case "Hardware"
                    QHardware(SourceFileName)
                    MessageBox.Show("Hardware")
                Case "Checked"
                    QChecked(SourceFileName)
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    QUnchecked(SourceFileName)
                    MessageBox.Show("Unchecked")
            End Select
        End If
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774186
Hi,

The format is as follows;

<?xml version="1.0" ?>
     </Hardware>
           <Hardware ISBN="1861005652" Title="Hardware list">
              <Type>Hardware</Type>
           </Hardware>
     </Hardware>




0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774191
Thats what I thought I should use, however I am new to VB, still learning!

Just trying it now...
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774200
I get an error on the line;

   Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

currentNodes is not declared?
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17774221
I dont think you'll require that line now, you can comment it out or remove it... unless there was a specific purpose for the array list?
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774337
Here is what I have now however nothing is happening, the program simply continues to run.  I have added some bad code to get a nullreference exception before the if statement, so I know the program is getting to the part.

Private Sub OpenXML(ByVal SourceFileName As String) ' Open XML document and run query based on XML node <Type></Type>

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName) 'SourceFilename will be something like c:\uploads\anXMLfile.xml

        Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("//Hardware/Hardware/Type")

        If (Not typeNode Is Nothing) Then
            Select Case typeNode.InnerText
                Case "Hardware"
                    MessageBox.Show("Hardware")
                Case "Checked"
                    MessageBox.Show("Checked")
                Case "Unchecked"
                    MessageBox.Show("Unchecked")
            End Select
        End If

    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17774392
this

Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("//Hardware/Hardware/Type")

should be either
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("//Type")

or
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/Hardware/Hardware/Type")

0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774487
Argh!!!.... still not doing anything with above suggestions....

Hm...
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774498
Ang on, think I might have spotted something.
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17774844
Nope, still cannot work it out.

I have altered all three of my xmldocuments now so that the format is;

<Audit>
    <Type>Hardware</Type>
    <Motherboard>Gigabyte</Motherboard>
    <CPU>Intel</CPU>
</Audit>
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17774860
use this

Dim typeNode As XmlNode = XmlDoc.DocumentElement.SelectSingleNode("/Audit/Type")

the xml node names are case sensitive ...
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17774996
did you try what I told you ?
0
 
LVL 35

Accepted Solution

by:
YZlat earned 250 total points
ID: 17776429
did you try

Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("//Type")
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17787031
Got it working now...


It was something totally daft, My filechecker looks through all folders for the oldest xml file, and then opens this.  The oldest file had old tags and therefore it would not run!

DOH!... Thanks very much for the help guys.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now