Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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 doesn't exist, the Count of the nodes returned will be 0.

 

For example:

 

'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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...