Need help on XPathNavigator reading XML (VB)

WaveRebel

New member
Joined
Jun 18, 2003
Messages
4
Im currently working with QBXML which is the same as XML 1.0 and its fully standard compliant.

I receive this XML from QuickBooks.
<?xml version="1.0" ?>
<QBXML>
<QBXMLMsgsRs>
<CustomerQueryRs requestID="1" statusCode="0" statusSeverity="Info" statusMessage="Status OK">
<CustomerRet>
<ListID>91D0000-1015432175</ListID>
<Name>01 @ Systems (New York)</Name>
<Contact>Rob Smiles</Contact>
</CustomerRet>
</CustomerQueryRs>
</QBXMLMsgsRs>
</QBXML>

I read it using XPathNavigator like this
Begin Xml
Dim CustXML As XmlDocument = New XmlDocument()
CustXML.LoadXml(response)
Dim nav As XPathNavigator = CustXML.CreateNavigator()

Setting XML Navigational Paths
Customer Information
Dim iListID As XPathNodeIterator = nav.Select(nav.Compile("/*/*/*/CustomerRet/ListID"))
Dim iName As XPathNodeIterator = nav.Select(nav.Compile("/*/*/*/CustomerRet/Name"))
Dim iContact As XPathNodeIterator = nav.Select(nav.Compile("/*/*/*/CustomerRet/Contact"))

Using ListID as Main iterator all Customer have a ListID according to Quickbooks
While (iListID.MoveNext())

Get Customer ID
ListID = iListID.Current.Clone.Value.ToString()

Get Customer Name
iName .MoveNext()
CustName = iName .Current.Clone.Value.ToString()

Get Customer Contact
iContact .MoveNext()
Contact = iContact .Current.Clone.Value.ToString()

End while


So the problem is QuickBooks only returns XML nodes out of filled fields.
Example: If in QuickBooks I have the field "Contact" empty it will not return the contact node in the XML.
So this leaves me with 1,300 Customers in a not good shape XML File which I cannot read since it skips nodes and gives me wrong values.

How can I detect if that portion of the xml doenst have than node?
I have like 300 Customers with "Contact" node not being in the XML.

Note: This is not the complete XML, its just an example of what is happening.
 
Instead of creating all three iterators at once, create an iterator
to loop through each CustomerRet node. Inside this loop, just get
the value for the ListID, Name, and Contact by using the
SelectChildren() method of the current CustomerRet node. If the
child node doesnt exist, the Count of the nodes returned will be 0.

For example:

Code:
Begin Xml
Dim CustXML As XmlDocument = New XmlDocument()
CustXML.LoadXml(response)
Dim nav As XPathNavigator = CustXML.CreateNavigator()

 Create an iterator for each customer
Dim iCustomer As XPathNodeIterator = nav.Select(nav.Compile("/*/*/*/CustomerRet"))

While iCustomer.MoveNext()  Loop through all customer nodes
   NOW you can get those child nodes
  Dim iListID As XPathNodeIterator = iCustomer.Current.SelectChildren("ListID","")
  Dim iName As XPathNodeIterator = iCustomer.Current.SelectChildren("Name","")
  Dim iContact As XPathNodeIterator = iCustomer.Current.SelectChildren("Contact","")

  If iListID.Count <> 0 Then  If a node is found....
     You *might* have to call iListID.MoveNext() here
    ListID = iListID.Current.Value  ... set the value to the string
  End If
   Do the above for each other value

End While

This code is untested, but it should give the gist of how you can
do it. You may need to use MoveNext before retrieving the
values of the child nodes. Good luck.
 
Thank you for your reply, as soon as I test it ill post the results here. But yes your correct using SelectChildren is the ticket.
 
Back
Top