Using XML in .NET August 2, 2008
Posted by tuse in : ASP.NET, Tools, VB.NET , 1 comment so farWe will see how we can use XML files in a VB.NET application. XML is used in many aspects of web development, often to simplify data storage.
In a Windows form, add a DataGridView and a Button. Change the text of this Button as ‘Save’.
The XML file that I am interested in-
John Smith
Computers
Matthew Max
Electrical
Jay Yan
Mechanical
The XML file that I have holds freshman records studying in an engineering school. We will look to display the fields ‘name’ and ‘branch’ in the DataGridView.
The ‘Save’ button should be able to save the changes made to the records in the DataGridView in the XML file.
We shall use a DataSet for reading the XML file. The Save Button will involve the Write function to the XML file invoked using the dataset.
The code-
Public Class Form1
Dim ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds = New DataSet
ds.ReadXml("C:\myxml\file1.xml") 'Path where your XML file exists on the system
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "freshman" 'Records that you wish to display. Look at the XML file for reference
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ds.WriteXml("C:\myxml\file1.xml") 'Save the changes made in the DataGridView to be reflected in the XML file
End Sub
End Class
This is an illustration of the usage of XML in VB.NET. You may use this in ASP.NET too. In order to specify the source for the file on the server, you may use ‘Server.MapPath()’ to specify the path for your XML file.
Console Applications August 1, 2008
Posted by tuse in : Console , add a commentVisual Studio lets you develop Console Applications as well. Console Applications are command-line applications similar to C or Java programs. These applications allow you to take input, display characters on a DOS window.
In order to develop a Console Application, start Visual Studio, point to New Project and select ‘Console Application’.
The System.Console namespace supports the creation of Console Applications.
As with C and Java, the program execution begins with the Sub Main() - the entry point of the program.
Module Module1
Sub Main()
Dim nm As String
System.Console.Write("Enter your name: ")
nm = System.Console.ReadLine()
System.Console.WriteLine("Your name is: " & nm)
System.Console.ReadLine()
End Sub
End Module
This simple program accepts a string from the user and displays it on the console. As you can see, the code is quite similar to a program written in Java. In order to execute this program, press F5/ Debug->Start.
A DOS window pops up asking you to enter your name. This is due to the ‘System.Console.Write()’‘ method.
Similarly, ‘System.Console.ReadLine()’ is used to read a line from the console. The final ‘System.Console.ReadLine()’ is like the getch() used in C programs so that the console ‘waits’ for a keypress before termination.