XML - how to check for the existence of an element and retrieve its value if found

Greetings Experts!

Working with vbscript and a very limited knowledge of XML files, I'm trying to create a function that gets several pieces of information from the MaterialPosting element of an XML file.  

As long as I stick to elements that 1) are always present in the XML and 2) have values directly under the /JobInfo/MaterialPosting element, such as StockCode, I can get what I need but I'm having trouble retrieving the values of items that are one level deeper, such as Bins and Serials.  I was able to figure out how to get the value of the Bins/Bin element (see line 21 of the code below). However, it doesn't work for serials because the Serials element does not always exist.  

After spending the better part of a day with the Microsoft docs on XML Dom, my head is spinning and I still don't get it.  The code below is my latest attempt.  It returns an error of "object doesn't support this property or method" at line 11.  

How can I check to see if the element called Serials exists in my XML and, if the Serial element exists, retrieve its value?

Many thanks, in advance, for your help!

Dim objDom, objNodeList, Counter, myXML
   Set objDom = CreateObject("MSXML2.DOMDocument")

   Set objNodeList = objDom.SelectNodes ("//JobInfo/MaterialPosting")
       If objNodeList.length > 0 then

  ' Loop through each of the MaterialPosting elements
      For Counter = 0 To objNodeList.length - 1
          dim aSerialNode
          aSerialNode = objNodeList(Counter).SelectSingleNode("//JobInfo/MaterialPosting/Serials")
		  If aSerialNode = nothing Then
		     msgbox "no serial node"
		     msgbox "serial node found"
		  End If
         MyXML = MyXML & objNodeList(Counter).SelectSingleNode("MStockCode").Text & Chr(255) &_
                 objNodeList(Counter).SelectSingleNode("MLot").Text & Chr(255) &_
	         objNodeList(Counter).SelectSingleNode("MQtyIssued").Text & Chr(255) &_
	   	 objNodeList(Counter).SelectSingleNode("./MBins/Bin").Text & vbCrLf
		   ' objNodeList(Counter).SelectSingleNode("./MSerials/Serial").Text & vbCrLf
   End If 

Open in new window

My XML file looks like this:

      <Value>          205.88</Value>
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

something like :
dim serial_node
set serial_node = objNodeList(Counter).SelectSingleNode("./MSerials/Serial")
if not serial_node is Nothing then MyXML = MyXML & serial_node.Text

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monkeybiz12345Author Commented:
This gets me going again.  Thank you zc2!
You are welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.