How to work with XML in Console Applications using VB.NET

In this article you will the connection of console application and xml sheet.
  • 2522
Download Files:
 

Extensible Markup Language (XML) is a meta-markup language which provides you a format for describing structured data. For data exchange on web XML is the universal language.

In this article I am creating a XML document and stored data into that and by using While in application code print the data on console window from the XML data source.

  1. First we create a Xml sheet and store data into that sheet.

    Sample Code

    <?xml version="1.0" standalone="yes"?>
    <
    DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
        <
    Products>
            <
    ProductID>1</ProductID>
            <
    ProductName>Chai</ProductName>
            <
    SupplierID>1</SupplierID>
            <
    CategoryID>1</CategoryID><QuantityPerUnit>20 boxes x 10 bags</QuantityPerUnit>
            <
    UnitPrice>58.0000</UnitPrice>
            <
    UnitsInStock>79</UnitsInStock>
            <
    UnitsOnOrder>0</UnitsOnOrder>
            <
    ReorderLevel>20</ReorderLevel>
            <
    Discontinued>false</Discontinued>
        </
    Products>
        <
    Products>
        <
    ProductID>2</ProductID>
            <
    ProductName>Chang</ProductName>
            <
    SupplierID>1</SupplierID>
            <
    CategoryID>1</CategoryID>
            <
    QuantityPerUnit>24 - 4 oz bottles</QuantityPerUnit>
            <
    UnitPrice>39.0000</UnitPrice>
            <
    UnitsInStock>17</UnitsInStock>
            <
    UnitsOnOrder>40</UnitsOnOrder>
            <
    ReorderLevel>25</ReorderLevel>
            <
    Discontinued>false</Discontinued>
        </
    Products>
        <
    Products>
            <
    ProductID>3</ProductID>
            <
    ProductName>sugar</ProductName>
            <
    SupplierID>1</SupplierID>
            <
    CategoryID>1</CategoryID>
            <
    QuantityPerUnit>100 bags</QuantityPerUnit>
            <
    UnitPrice>42.0000</UnitPrice>
            <
    UnitsInStock>17</UnitsInStock>
            <
    UnitsOnOrder>40</UnitsOnOrder>
            <
    ReorderLevel>25</ReorderLevel>
            <
    Discontinued>false</Discontinued>
        </
    Products>
        <
    Products>
            <
    ProductID>4</ProductID>
            <
    ProductName>rice</ProductName>
            <
    SupplierID>1</SupplierID>
            <
    CategoryID>1</CategoryID>
            <
    QuantityPerUnit>30 kg</QuantityPerUnit>
            <
    UnitPrice>19.0000</UnitPrice>
            <
    UnitsInStock>17</UnitsInStock>
            <
    UnitsOnOrder>40</UnitsOnOrder>
            <
    ReorderLevel>25</ReorderLevel>
            <
    Discontinued>false</Discontinued>
        </
    Products>
        <
    Products>
            <
    ProductID>5</ProductID>
            <
    ProductName>oil</ProductName>
            <
    SupplierID>1</SupplierID>
            <
    CategoryID>1</CategoryID>
            <
    QuantityPerUnit>34 - 10 oz bottles</QuantityPerUnit>
            <
    UnitPrice>19.0000</UnitPrice>
            <
    UnitsInStock>17</UnitsInStock>
            <
    UnitsOnOrder>40</UnitsOnOrder>
            <
    ReorderLevel>25</ReorderLevel>
            <
    Discontinued>false</Discontinued>
        </
    Products>
    </
    DataSet1>

     

  2. Now we create a Console Application in VB.NET and using IF....ELSE and While loop within the design code.

    Sample Code

    Module MainModule

        Private dsProducts As New DataSet
        Private dvProducts As DataView

        Private promptMessage As String = "Enter a product ID to display information or 'QUIT' to end this application."

        Sub Main()
            Dim input As String
            Dim
    data As Integer
            CreateDataSet()
            Console.WriteLine()
            Console.WriteLine(promptMessage)
            input = UCase(
    Console.ReadLine())
            While input <> "QUIT"
                While Not (IsNumeric(input) Or UCase(input) = "QUIT")
                    Console.WriteLine("A numeric product ID is required.")
                    Console.WriteLine("Please reenter the Product ID or QUIT to continue.")
                    input =
    Console.ReadLine()
                End While
                If
    UCase(input) = "QUIT" Then
                    End
                End
    If
                data = dvProducts.Find(input)
                If data = -1 Then
                    Console.WriteLine("No product found.")
                Else
                    Console.Write("Product Name: ")
                    Console.WriteLine(dvProducts(data)("ProductName"))
                    Console.Write("Quantity Per Unit: ")
                    Console.WriteLine(dvProducts(data)("QuantityPerUnit"))
                    Console.Write("Unit Price: ")
                    Console.WriteLine(dvProducts(data)("UnitPrice"))
                    Console.Write("Units In Stock: ")
                    Console.WriteLine(dvProducts(data)("UnitsInStock"))
                    Console.Write("Units on Order: ")
                    Console.WriteLine(dvProducts(data)("UnitsOnOrder"))
                    Console.Write("Reorder Level: ")
                    Console.WriteLine(dvProducts(data)("ReorderLevel"))
                    Console.Write("Discontinued: ")
                    If CBool(dvProducts(data)("Discontinued")) = False Then
                        Console.WriteLine("False")
                    Else
                        Console.WriteLine("True")
                    End If
                End
    If
                Console.WriteLine()
                Console.WriteLine(promptMessage)
                input = UCase(
    Console.ReadLine())
            End While
            End
        End
    Sub

        Private
    Sub CreateDataSet()
            Dim thisExe As System.Reflection.Assembly
            thisExe = System.Reflection.Assembly.GetExecutingAssembly()
            Dim xmlFile As System.IO.Stream
            xmlFile = thisExe.GetManifestResourceStream("ConsoleApp.Products.xml")
            dsProducts.ReadXml(xmlFile)
            dvProducts =
    New DataView(dsProducts.Tables(0))
            dvProducts.Sort =
    "ProductID"
        End Sub
    End
    Module

  3. Debug the application and input your choice from 1 to 5 in console application, result will shows on same window.

    Output
    11.gif

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.