Skip to content

Instantly share code, notes, and snippets.

@trashvin
Last active November 10, 2019 16:08
Show Gist options
  • Save trashvin/a9c10550b99f3835b501442340d9da61 to your computer and use it in GitHub Desktop.
Save trashvin/a9c10550b99f3835b501442340d9da61 to your computer and use it in GitHub Desktop.
List processing example VB.NET
Imports System.IO
Imports System.Collections.Generic
Imports System.Xml
Module Module1
Sub Main()
' Read the sample file and populate list
Dim taxes As List(Of Tax) = GetData("xDataSample.txt")
Dim processedTaxes As List(Of Tax) = ProcessTaxes(taxes)
Dim taxXml As XmlDocument = CreateTaxXml(processedTaxes)
System.Console.WriteLine(taxXml.OuterXml)
System.Console.Read()
End Sub
Private Function GetData(ByVal fileName As String) As List(Of Tax)
Dim listOfTax As New List(Of Tax)
Dim fileContent As String = File.ReadAllText(fileName)
Dim taxEntries() As String = fileContent.Split(vbCrLf)
For Each taxEntry As String In taxEntries
Dim taxLine() = taxEntry.Split(";")
Dim tempTax As New Tax
If taxLine.Length > 1 Then
tempTax.TaxType = taxLine(0).Trim()
tempTax.TaxRate = taxLine(1).Trim()
tempTax.Amount = Int32.Parse(taxLine(2))
listOfTax.Add(tempTax)
End If
Next
GetData = listOfTax
End Function
Private Function CreateTaxXml(ByVal taxes As List(Of Tax)) As XmlDocument
Dim doc As New XmlDocument
Dim root As XmlNode = doc.CreateElement("Taxes")
Dim dictTaxes As New Dictionary(Of String, List(Of XmlElement))
For Each taxEntry In taxes
Dim innerNode As XmlElement = doc.CreateElement("Rate")
innerNode.SetAttribute("Percentage", taxEntry.TaxRate)
innerNode.SetAttribute("Amount", taxEntry.Amount)
If dictTaxes.ContainsKey(taxEntry.TaxType) Then
dictTaxes(taxEntry.TaxType).Add(innerNode)
Else
dictTaxes.Add(taxEntry.TaxType, New List(Of XmlElement))
dictTaxes(taxEntry.TaxType).Add(innerNode)
End If
Next
For Each keyValue As KeyValuePair(Of String, List(Of XmlElement)) In dictTaxes
Dim node As XmlNode = doc.CreateElement(keyValue.Key)
For Each element As XmlElement In keyValue.Value
node.AppendChild(element)
Next
root.AppendChild(node)
Next
doc.AppendChild(root)
CreateTaxXml = doc
End Function
Private Function ProcessTaxes(ByVal taxes As List(Of Tax)) As List(Of Tax)
Dim taxTypes As New List(Of Tax)
For Each taxEntry In taxes
If taxTypes.Any(Function(x) x.TaxType = taxEntry.TaxType _
AndAlso x.TaxRate = taxEntry.TaxRate) Then
Dim index As Integer =
taxTypes.IndexOf(
taxTypes.Where(Function(x) x.TaxType = taxEntry.TaxType _
AndAlso x.TaxRate = taxEntry.TaxRate) _
.First()
)
taxEntry.Amount += taxTypes(index).Amount
taxTypes(index) = taxEntry
Else
taxTypes.Add(taxEntry)
End If
Next
ProcessTaxes = taxTypes
End Function
End Module
Public Structure Tax
Public TaxType As String
Public TaxRate As String
Public Amount As Integer
End Structure
<Taxes>
<Tax1>
<Rate Percentage="20%" Amount="1" />
<Rate Percentage="10%" Amount="4" />
</Tax1>
<Tax2>
<Rate Percentage="5%" Amount="6" />
<Rate Percentage="10%" Amount="2" />
</Tax2>
<Tax3>
<Rate Percentage="15%" Amount="3" />
</Tax3>
<Tax4>
<Rate Percentage="10%" Amount="7" />
</Tax4>
</Taxes>
Tax1;20%;1
Tax1;10%;2
Tax2;5%;1
Tax2;5%;3
Tax2;10%;2
Tax1;10%;2
Tax3;15%;3
Tax2;5%;2
Tax4;10%;7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment