Solved

How to parse this XML document in vb.net

Posted on 2011-02-28
4
459 Views
Last Modified: 2012-06-27
Hello,

I have this xmlDocument and I need to parse the Attribute: step:AcctPlayBack and get the value in it in vb.net

<?xml version="1.0"?>
      <state>
            <variable name="call_id" value="1298923172.61383" />
            <variable name="unique_id"" value=""1298923172.61383" />
            <variable name="dnis" value="13456789" />
            <variable name="callerid" value="123456789" />
            <variable name="callerid_name" value="username" />
            <variable name="step:9" value=""3132278"" />
            <variable name="step:AccountEntry" value="3132278" />
            <variable name="step:17" value="2" />
            <variable name="step:AcctPlayBack" value="2" />
      </state>
</xml>


I tried working with attributes but I am getting errors, how do I parse the value for that attribute?

Thanks
0
Comment
Question by:htabbach
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35002291
try linq to xml

                Dim MyDoc As XDocument = XDocument.Load("Path2File")
                Dim xString = From seg In MyDoc...<state> Where seg.@<name> = "step:AcctPlayBack" Select seg.<Value>

Open in new window

0
 

Author Comment

by:htabbach
ID: 35002336
               Dim MyDoc As XDocument = XDocument.Load("Path2File")
                Dim xString = From seg In MyDoc...<state> Where seg.@<name> = "step:AcctPlayBack" Select seg.<Value>

I am sorry bit I have no idea what that was.
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 35002576
linq to xml
you will find your value in xString
NOTE: xString is a String(), and if there is more than one element whose attribute value for name is ....., then their  value attribute value will be in xString

PS> Change the second line to

Dim xString = From seg In MyDoc...<state> Where seg.@<name> = "step:AcctPlayBack" Select seg.@<Value>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35036938
Try the following:
Imports System.Xml

...

Dim xdoc As New XmlDocument
Dim value As String = String.Empty

xdoc.Load("filename.xml")

Dim node As XmlNode = xdoc.SelectSingleNode("//variable[@name='step:AcctPlayBack']/@value")

If node IsNot Nothing Then
    value = node.InnerText
End If

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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