Values from XPathDocument

Hi I am working on a managed stored proc and came across a situation where I am lacking basics.

I want to be able to pass an xml-content as string and then get all the values of child elements of VoucherLine and store them into any variables.

example

a = AccountID
b = NetAmount ....

Imports System.Xml

Imports System.Xml.XPath

Module Module1

Sub Main()

GetValues("<VoucherDetails><Lines><VoucherLine><AccountID>3e58a9f2-565b-4d49-b9fe-9d0d95ab9041</AccountID><LineText>Details</LineText><NetAmount>4.00</NetAmount><TaxValue>20</TaxValue><PrivateLine>false</PrivateLine></VoucherLine></Lines></VoucherDetails>")

End Sub

Public Function GetValues(ByVal xml As String)

Dim xmlDoc As New XmlDocument

xmlDoc.LoadXml(xml.ToString)

Dim doc As New XPathDocument(New XmlNodeReader(xmlDoc.DocumentElement))

Dim nav As XPathNavigator = doc.CreateNavigator

Dim nodes As XPathNodeIterator = nav.Select("//VoucherDetails")

Dim a, b, c As String

While nodes.MoveNext

a = nodes.Current.Evaluate("AccountID/text()")

b = nodes.Current.Evaluate("NetAmount/text()")

End While

End Function

Thanks for batching me up :)



Answer this question

Values from XPathDocument

  • itznfb

    VoucherDetails is the root element so you don't actually have to select it:

    Dim xmlstr As String = "<VoucherDetails><Lines><VoucherLine><AccountID>3e58a9f2-565b-4d49-b9fe-9d0d95ab9041</AccountID><LineText>Details</LineText><NetAmount>4.00</NetAmount><TaxValue>20</TaxValue><PrivateLine>false</PrivateLine></VoucherLine></Lines></VoucherDetails>"
    Dim doc As New XmlDocument
    doc.LoadXml(xmlstr)

    Dim a, b As String

    For Each node As XmlNode In doc.SelectNodes("//Lines")
    a = node.SelectSingleNode("descendant::AccountID").InnerText
    b = node.SelectSingleNode("descendant::NetAmount").InnerText
    Debug.WriteLine("AccountID:" & a & vbCrLf & "NetAmount: " & b)
    Next



  • pc_bond

    Thanks a lot. Even though I am still curious about how to use the evaluate methode of the xpathdocument.

    Alex


  • Values from XPathDocument