Capture Element Attributes in XML using VB.Net

I need a way of looping through an XML document and obtaining values using for the following: PHYSICALADDRESS:Street; PHYSICALADDRESS:city; PHYSICALADDRESS:state;TAXID:SSN;DOB:dob. I would like these values in seperate string variable so I can use later. See the attached XML document.
Stacey FontenotAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try customize this:

Dim xdoc As Xml.XmlDocument = New Xml.XmlDocument

        Dim nsmgr As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(xdoc.NameTable)
        nsmgr.AddNamespace("at", "urn:v556")

        Dim node As Xml.XmlNode = xdoc.SelectSingleNode("//at:CLIENTDATA/at:INDIVIDUAL", nsmgr)
        Dim street1 As String = node.SelectSingleNode("at:CONTACTINFO/at:PHYSICALADDRESS", nsmgr).Attributes("street1").InnerText
        Dim street2 As String = node.SelectSingleNode("at:CONTACTINFO/at:PHYSICALADDRESS", nsmgr).Attributes("street2").InnerText
        Dim city As String = node.SelectSingleNode("at:CONTACTINFO/at:PHYSICALADDRESS", nsmgr).Attributes("city").InnerText
        Dim state As String = node.SelectSingleNode("at:CONTACTINFO/at:PHYSICALADDRESS", nsmgr).Attributes("state").InnerText
        Dim ssn As String = node.SelectSingleNode("at:IDINFO/at:TAXID", nsmgr).Attributes("ssn").InnerText
        Dim dob As String = node.SelectSingleNode("at:IDINFO/at:DOB", nsmgr).Attributes("dob").InnerText

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
Fernando SotoRetiredCommented:
Hi Stacey;

Here is a code snippet that should meet your needs.
'' Load the XML file
Dim xdoc = XDocument.Load("Path and file name here")
'' XML Namespace needed to access the nodes
Dim at As XNamespace = "urn:v556"
'' Query the information and create a class object with the fields populated
Dim results = (From n In xdoc.Root.Descendants(at + "INDIVIDUAL")
               Let address = n.Element(at + "CONTACTINFO").Element(at + "PHYSICALADDRESS") 
               Let street1 = address.Attribute("street1").Value.Trim
               Let street2 = address.Attribute("street2").Value.Trim
               Let city = address.Attribute("city").Value.Trim
               Let state = address.Attribute("state").Value.Trim
               Let idinfo = n.Element(at + "IDINFO")
               Let taxId = idinfo.Element(at + "TAXID").Attribute("ssn").Value.Trim
               Let dob = DateTime.Parse(idinfo.Element(at + "DOB").Attribute("dob").Value.Trim)
               Select New Individual() With
                .TaxId = taxId,
                .Dob = dob,
                .Street1 = street1,
                .Street2 = street2,
                .City = city,
                .State = state

'' Access the data
For Each item In results
    Console.WriteLine("{0} : {1} : {2} : {3} : {4} : {5}", item.TaxId, item.Dob, item.Street1, item.Street2, item.City, item.State)

'' Class used to return the results
Public Class Individual
    Public Property Street1 As String
    Public Property Street2 As String
    Public Property City As String
    Public Property State As String
    Public Property TaxId As String
    Public Property Dob As DateTime
End Class

Open in new window

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
Visual Basic.NET

From novice to tech pro — start learning today.