Link to home
Create AccountLog in
Avatar of mdreed
mdreedFlag for United States of America

asked on

How to parse XML address verification response from USPS in VB.NET?

I have successfully sent and received an address validation request to and from USPS.  The response comes back as an XML string, as it should.

Is there some code, wrapper, module, etc. out there that will parse the response elements so that I can easily retrieve street, city, state, zip, etc.?
Avatar of mdreed

ASKER

The examples, although staightforward, seem to require an XML file path.  I already have the USPS XML string (file contents) in a variable.  How can I use the examples with my variable contents instead of a file path?
Thanks.
http://msdn.microsoft.com/en-us/library/1af7xa52.aspx

XMLTextReader can take a file as input, or a STRING.
Avatar of mdreed

ASKER

I have read the link you provided, but it appears to me that XMLTextReader expects a file name (file path) and not a string variable.

What I have been working with is the following, but I need to parse the XML contents of a string variable instead of an XML file ("H:\Sample.xml").

Dim xDoc As XmlDocument = New XmlDocument()
xDoc.Load("H:\Sample.xml")
Dim Address1 As XmlNodeList = xDoc.GetElementsByTagName("Address1")
Dim Address2 As XmlNodeList = xDoc.GetElementsByTagName("Address2")
Dim City1 As XmlNodeList = xDoc.GetElementsByTagName("City")
Dim ST1 As XmlNodeList = xDoc.GetElementsByTagName("State")
Dim Zip1 As XmlNodeList = xDoc.GetElementsByTagName("Zip5")
Dim Zip2 As XmlNodeList = xDoc.GetElementsByTagName("Zip4")
MessageBox.Show(Address2(0).InnerText & Constants.vbLf & _
                        City1(0).InnerText & ", " & ST1(0).InnerText & " " & _                       Zip1(0).InnerText & "-" & Zip2(0).InnerText)

This code works great, but it won't handle a string variable instead of a file name.  I can change it to use the XMLTextReader, but I don't see how it would work either.  Can you provide and example of how to do as I have done above but with XMLTextReader.
ASKER CERTIFIED SOLUTION
Avatar of sweetfa2
sweetfa2
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mdreed

ASKER

Works very well with the code I am using to extract the nodes.  Thanks.