VB.NET

VB.Net : Check If Given String is EmailID

Public Function IsEmail(ByVal email As String) As Boolean
Dim emailRegex As New Regex(“^([\w-\.]+)@([\w-]+\.)+([\w-]{2,4})$”)

If emailRegex.IsMatch(email) Then
Return True
Else
Return False
End If
End Function

VB.NET : Get Random Number

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

vb.net : Handling JSON

Check If Valid JSON

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)

Console.WriteLine(“Name: ” & obj(“name”))
Console.WriteLine(“Age: ” & obj(“age”))
Console.WriteLine(“City: ” & obj(“city”))

Strongly Typed

Imports Newtonsoft.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)

Console.WriteLine(“Name: ” & person.Name)
Console.WriteLine(“Age: ” & person.Age)
Console.WriteLine(“City: ” & person.City)

VB.NET : XML To JSON

Imports Newtonsoft.Json
Imports System.Xml

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

VB.Net : Read Settings in WinForm

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

My.Settings.SettingName = “NewValue”
My.Settings.Save()

Note that you need to call the Save method after changing a setting value to persist the change.

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