VB.Net : Parse XML

Imports System.Xml

Private Sub ParseXML()
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(“NavaFP01000012022-12-170.00.00.0S C 10 %0.0C.Gst 2.5 %0.0S.Gst 2.5 %0.0NcSale“)

Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode(“Records/Record”)
If xmlNode IsNot Nothing Then
Dim posName As String = xmlNode(“POSName”).InnerText
Dim billNo As String = xmlNode(“BillNo”).InnerText
Dim date As String = xmlNode(“Date”).InnerText
Dim time As String = xmlNode(“Time”).InnerText
Dim subTotal As String = xmlNode(“SubTotal”).InnerText
Dim discountAmount As String = xmlNode(“DiscountAmount”).InnerText
Dim grandTotalAmount As String = xmlNode(“GrandTotalAmount”).InnerText

‘ Access the Taxes node
Dim taxesNode As XmlNode = xmlNode(“Taxes”)
For Each taxNode As XmlNode In taxesNode.ChildNodes
Dim taxDesciption As String = taxNode(“TaxDesciption”).InnerText
Dim taxAmount As String = taxNode(“TaxAmount”).InnerText
Next

Dim settelmentMode As String = xmlNode(“SettelementMode”).InnerText
Dim transactionStatus As String = xmlNode(“TransactionStatus”).InnerText
End If
End Sub

Public Class Record
Public Property POSName As String
Public Property BillNo As String
Public Property Date As String
Public Property Time As String
Public Property SubTotal As Decimal
Public Property DiscountAmount As Decimal
Public Property GrandTotalAmount As Decimal
Public Property Taxes As List(Of Tax)
Public Property SettelementMode As String
Public Property TransactionStatus As String
End Class

Public Class Tax
Public Property TaxDesciption As String
Public Property TaxAmount As Decimal
End Class

Imports System.Xml

Private Function ParseXML(xmlString As String) As Record
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(xmlString)

Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode(“Records/Record”)
If xmlNode IsNot Nothing Then
Dim record As New Record()
record.POSName = xmlNode(“POSName”).InnerText
record.BillNo = xmlNode(“BillNo”).InnerText
record.Date = xmlNode(“Date”).InnerText
record.Time = xmlNode(“Time”).InnerText
record.SubTotal = Decimal.Parse(xmlNode(“SubTotal”).InnerText)
record.DiscountAmount = Decimal.Parse(xmlNode(“DiscountAmount”).InnerText)
record.GrandTotalAmount = Decimal.Parse(xmlNode(“GrandTotalAmount”).InnerText)

‘ Access the Taxes node
Dim taxesNode As XmlNode = xmlNode(“Taxes”)
record.Taxes = New List(Of Tax)()
For Each taxNode As XmlNode In taxesNode.ChildNodes
Dim tax As New Tax()
tax.TaxDesciption = taxNode(“TaxDesciption”).InnerText
tax.TaxAmount = Decimal.Parse(taxNode(“TaxAmount”).InnerText)
record.Taxes.Add(tax)
Next

record.SettelmentMode = xmlNode(“SettelementMode”).InnerText
record.TransactionStatus = xmlNode(“TransactionStatus”).InnerText

Return record
End If

Return Nothing
End Function

You can use the System.Net.Http namespace in .NET to send a REST API GET request and retrieve the XML response. To parse the XML response into a class object, you can use the code I provided in my previous answer.

Imports System.Net.Http

Private Async Function GetXMLAsync() As Task(Of String)
Dim client As New HttpClient()
Dim response As HttpResponseMessage = Await client.GetAsync(“https://your-api-endpoint.com”)

If response.IsSuccessStatusCode Then
Dim xmlString As String = Await response.Content.ReadAsStringAsync()
Return xmlString
End If

Return Nothing
End Function

Private Async Sub LoadXMLAsync()
Dim xmlString As String = Await GetXMLAsync()
If Not String.IsNullOrEmpty(xmlString) Then
Dim record As Record = ParseXML(xmlString)
‘ Do something with the Record object
End If
End Sub

Listing All Records

Private Function ParseXML(xmlString As String) As List(Of Record)
Dim records As New List(Of Record)()

Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(xmlString)

Dim recordNodes As XmlNodeList = xmlDoc.SelectNodes(“/Records/Record”)
For Each recordNode As XmlNode In recordNodes
Dim record As New Record()

record.POSName = recordNode.SelectSingleNode(“POSName”).InnerText
record.BillNo = recordNode.SelectSingleNode(“BillNo”).InnerText
record.Date = DateTime.Parse(recordNode.SelectSingleNode(“Date”).InnerText)
record.Time = DateTime.Parse(recordNode.SelectSingleNode(“Time”).InnerText)
record.SubTotal = Decimal.Parse(recordNode.SelectSingleNode(“SubTotal”).InnerText)
record.DiscountAmount = Decimal.Parse(recordNode.SelectSingleNode(“DiscountAmount”).InnerText)
record.GrandTotalAmount = Decimal.Parse(recordNode.SelectSingleNode(“GrandTotalAmount”).InnerText)

Dim taxNodes As XmlNodeList = recordNode.SelectNodes(“Taxes/Tax”)
For Each taxNode As XmlNode In taxNodes
Dim tax As New Tax()
tax.TaxDesciption = taxNode.SelectSingleNode(“TaxDesciption”).InnerText
tax.TaxAmount = Decimal.Parse(taxNode.SelectSingleNode(“TaxAmount”).InnerText)

record.Taxes.Add(tax)
Next

record.SettelementMode = recordNode.SelectSingleNode(“SettelementMode”).InnerText
record.TransactionStatus = recordNode.SelectSingleNode(“TransactionStatus”).InnerText

records.Add(record)
Next

Return records
End Function

Scroll to Top