• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 64
  • Last Modified:

Capture Element Attributes in XML using VB.Net

I need a way of looping through an XML document and obtaining values using VB.net 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 Fontenot
Stacey Fontenot
1 Solution
Ryan ChongCommented:
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

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now