Solved

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

Posted on 2010-11-09
6
980 Views
Last Modified: 2012-06-21
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.?
0
Comment
Question by:mdreed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34096771
0
 

Author Comment

by:mdreed
ID: 34102952
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.
0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34185887
http://msdn.microsoft.com/en-us/library/1af7xa52.aspx

XMLTextReader can take a file as input, or a STRING.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:mdreed
ID: 34190652
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.
0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 500 total points
ID: 34191293
XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
0
 

Author Closing Comment

by:mdreed
ID: 34196688
Works very well with the code I am using to extract the nodes.  Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question