?
Solved

get value from xml wit vb.net

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

<?xml version="1.0" encoding="UTF-8"?>
<response>
      <contacts>
            <contact>
                  
                  <contactId>66253009</contactId>
                  <alerts>1</alerts>
                  <discounts>1</discounts>
                  <userid>146764d2-bb86-4228-85be-87e050523ae6</userid>
                  <userstatus>User</userstatus>
            </contact>
      </contacts>
      <limit>20</limit>
      <offset>0</offset>
      <total>1</total>
</response>
0
Comment
Question by:Sitewizard
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36969370
0
 

Author Comment

by:Sitewizard
ID: 36969495
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
            Next
        Next

returns nothing to me
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36969525
Because ContactID is a child node not attribute, use the second loop which is

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

Author Closing Comment

by:Sitewizard
ID: 36969667
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
            Next
        Next

        Return contactid
    End Function
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

578 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