This function creates a new instance of the Random class, then uses the Next method to generate a random integer between 999 and 9999. Finally, the function returns the generated random number.
You can call this function in your code to generate a random number within the specified range. For example:
Public Function GenerateRandomNumber() As Integer
Dim rnd As New Random()
Dim randomNumber As Integer = rnd.Next(1000, 9999)
Return randomNumber
End Function
Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JsonConvert.DeserializeObject(json)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JToken.Parse(json)
Return True
Catch ex As Exception
Return False
End Try
End Function
Check If A Key Exists
Public Shared Function KeyExists(ByVal json As String, ByVal key As String) As Boolean
Try
Dim obj As JObject = JObject.Parse(json)
Return obj.ContainsKey(key)
Catch ex As Exception
Return False
End Try
End Function
Get Value Of A Key if Valid JSON and Key Exists
Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Dim Rtn$ = “”
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Rtn = prop.Value.ToString()
End If
Catch ex As Exception
Rtn = “”
End Try
Return Await Task.FromResult(Of String)(Rtn)
End Function
Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
If obj.ContainsKey(key) Then
Return obj(key).ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Uses :
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim result As String = Await JSONHelper.GetValueFromJSONAsync(json, “name”)
Console.WriteLine(result)
Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function
Dim jsonString As String = “{ “”body””:””om sai””, “”type””:””chat””, “”notifyName””:””US””, “”from””:””919407755200@c.us””, “”to””:””919406214451@c.us””}”
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
Dim fromValue As String = jsonObject(“from”).ToString()
Console.WriteLine(fromValue)
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim obj As Object = JsonConvert.DeserializeObject(json)
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Property City As String
End Class
Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim person As Person = JsonConvert.DeserializeObject(Of Person)(json)
Module XMLtoJSON
Sub Main()
‘ Sample XML
Dim xml As String = “NavaFP01004882023-01-307000.00.08050.0S C 10 %700.0C.Gst 2.5 %175.0S.Gst 2.5 %175.0Credit CardSale”
‘ Load XML to XmlDocument object
Dim doc As New XmlDocument()
doc.LoadXml(xml)
‘ Convert XmlDocument to Json string
Dim json As String = JsonConvert.SerializeXmlNode(doc)
‘ Print the Json string
Console.WriteLine(json)
Console.ReadLine()
End Sub
End Module
To read settings in a VB.NET WinForm application, you can use the My.Settings object. My.Settings is a strongly typed class that provides access to the settings defined in the project’s Settings.settings file.
Here’s an example of how you can use My.Settings to read a setting:
Dim settingValue As String = My.Settings.SettingName
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
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()
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)