nickmarshall
asked on
XML select node, then select case not working
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(currentNo des)
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.
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
Dim currentNode As XmlNode
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
Dim currentArray As ArrayList = NodesToArrayList(currentNo
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.
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(currentNo des)
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
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
Dim currentNode As XmlNode
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
Dim currentArray As ArrayList = NodesToArrayList(currentNo
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
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(currentNo des)
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
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
Dim currentNode As XmlNode
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
Dim currentArray As ArrayList = NodesToArrayList(currentNo
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
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
And not to mention I feel bad for it not working from the first question!
Dez
ASKER
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(currentNo des)
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.
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
Dim currentNode As XmlNode
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
Dim currentArray As ArrayList = NodesToArrayList(currentNo
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.
ASKER
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)
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)
"hardware" or "Hardware"?
let me see your XML
let me see your XML
ASKER
<?xml version="1.0" ?>
- <Hardware>
- <Hardware ISBN="1861005652" Title="Hardware list">
<Type>Hardware</Type>
- <CPU>
<CurrentClockSpeed>1716</C urrentCloc kSpeed>
<Manufacturer>GenuineIntel </Manufact urer>
<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|\D evice\Hard disk0\Part ition2</Na me>
<Organization>MyOrg</Organ ization>
<RegisteredUser>The Hostmaster</RegisteredUser >
<SerialNumber>55274-OEM-00 14286-7777 </SerialNu mber>
<TotalVisibleMemorySize>10 48052</Tot alVisibleM emorySize>
<WindowsDirectory>F:\WINDO WS</Window sDirectory >
</System>
- <Sound>
<Manufacturer>Intel</Manuf acturer>
<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.410 9</DriverV ersion>
<Name>NVIDIA Quadro4 NVS</Name>
</DisplayAdapter>
- <DisplayAdapter>
<DriverVersion>4.00.1636.1 </DriverVe rsion>
<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</FileSyst em>
<FreeSpace>487625728</Free Space>
<Size>2097413632</Size>
</LogicalDrives>
- <LogicalDrives>
<DeviceID>D:</DeviceID>
</LogicalDrives>
- <LogicalDrives>
<DeviceID>F:</DeviceID>
<FileSystem>NTFS</FileSyst em>
<FreeSpace>21185536000</Fr eeSpace>
<Size>37918507008</Size>
</LogicalDrives>
</Hardware>
</Hardware>
- <Hardware>
- <Hardware ISBN="1861005652" Title="Hardware list">
<Type>Hardware</Type>
- <CPU>
<CurrentClockSpeed>1716</C
<Manufacturer>GenuineIntel
<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|\D
<Organization>MyOrg</Organ
<RegisteredUser>The Hostmaster</RegisteredUser
<SerialNumber>55274-OEM-00
<TotalVisibleMemorySize>10
<WindowsDirectory>F:\WINDO
</System>
- <Sound>
<Manufacturer>Intel</Manuf
<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.410
<Name>NVIDIA Quadro4 NVS</Name>
</DisplayAdapter>
- <DisplayAdapter>
<DriverVersion>4.00.1636.1
<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</FileSyst
<FreeSpace>487625728</Free
<Size>2097413632</Size>
</LogicalDrives>
- <LogicalDrives>
<DeviceID>D:</DeviceID>
</LogicalDrives>
- <LogicalDrives>
<DeviceID>F:</DeviceID>
<FileSystem>NTFS</FileSyst
<FreeSpace>21185536000</Fr
<Size>37918507008</Size>
</LogicalDrives>
</Hardware>
</Hardware>
ASKER
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.
In all the Xml files that I am checking, there will only be one type node.
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
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
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.Sel ectNodes(" /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.Sel ectNodes(" /TYPE") '<--- Here I am trying to specify the node, which is simply <Type></Type>
Hope this Helps :)
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//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.Sel
if XML is like this
<TYPE>
</TYPE>
<TYPE>
</TYPE>
Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.Sel
Hope this Helps :)
this case you should write "/Hardware/Hardware/Type"
Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.Sel ectNodes(" /Hardware/ Hardware/T ype") '<--- Here I am trying to specify the node, which is simply <Type></Type>
Dim currentNodes As XmlNodeList = XmlDoc.DocumentElement.Sel
change
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type /Type")
to
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type ")
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
to
Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type
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/T ype")
Dim currentArray As ArrayList = NodesToArrayList(currentNo des)
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
Dim XmlDoc As New XmlDocument
XmlDoc.Load(SourceFileName
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/
Dim currentArray As ArrayList = NodesToArrayList(currentNo
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
ASKER
Hi,
The format is as follows;
<?xml version="1.0" ?>
</Hardware>
<Hardware ISBN="1861005652" Title="Hardware list">
<Type>Hardware</Type>
</Hardware>
</Hardware>
The format is as follows;
<?xml version="1.0" ?>
</Hardware>
<Hardware ISBN="1861005652" Title="Hardware list">
<Type>Hardware</Type>
</Hardware>
</Hardware>
ASKER
Thats what I thought I should use, however I am new to VB, still learning!
Just trying it now...
Just trying it now...
ASKER
I get an error on the line;
Dim currentArray As ArrayList = NodesToArrayList(currentNo des)
currentNodes is not declared?
Dim currentArray As ArrayList = NodesToArrayList(currentNo
currentNodes is not declared?
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?
ASKER
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/T ype")
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
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
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/
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
this
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/ /Hardware/ Hardware/T ype")
should be either
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/ /Type")
or
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/ Hardware/H ardware/Ty pe")
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/
should be either
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/
or
Dim typeNode As XmlNode = XmlDoc.SelectSingleNode("/
ASKER
Argh!!!.... still not doing anything with above suggestions....
Hm...
Hm...
ASKER
Ang on, think I might have spotted something.
ASKER
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</Mot herboard>
<CPU>Intel</CPU>
</Audit>
I have altered all three of my xmldocuments now so that the format is;
<Audit>
<Type>Hardware</Type>
<Motherboard>Gigabyte</Mot
<CPU>Intel</CPU>
</Audit>
use this
Dim typeNode As XmlNode = XmlDoc.DocumentElement.Sel ectSingleN ode("/Audi t/Type")
the xml node names are case sensitive ...
Dim typeNode As XmlNode = XmlDoc.DocumentElement.Sel
the xml node names are case sensitive ...
did you try what I told you ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
MessageBox.Show(currentNod
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?