get value from xml wit

Posted on 2011-10-14
Last Modified: 2012-05-12
I am simply trying to get the value for contactid from xml with  The xml document will only have one contact listed and look will be layed out as follows:

<?xml version="1.0" encoding="UTF-8"?>
Question by:Sitewizard
    LVL 83

    Expert Comment


    Author Comment

    I must not understand how to get down to the correct node or im just not using the exaple correclty.  As I said I just want the Value from the contactId node

     Dim doc = XDocument.Load(New StringReader(xmlstring))
            For Each myCon In doc...<contact>
                For Each a In myCon.Attributes
                    If a.Name.ToString = "contactId" Then
                        contactid = (a.Name.ToString & " : " & a.Value)
                    End If

    returns nothing to me
    LVL 83

    Accepted Solution

    Because ContactID is a child node not attribute, use the second loop which is

    For each b in myCon.Descendants
          ContactID = b.value

    Author Closing Comment

    this worked great:

    Final Code:  

      Function main2(ByVal xmlstring As String) As String
            Dim contactid As String = "not"
            Dim doc = XDocument.Load(New StringReader(xmlstring))
            For Each myCon In doc...<contact>
                For Each b In myCon.Descendants
                    If b.Name.ToString = "contactId" Then
                        contactid = b.Value
                    End If

            Return contactid
        End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now