|
|
|
|
|
The Microsoft
Visual Basic .Net member of the Microsoft Visual Studio .Net |
|
Xml-Book application ..., describe how to work with XML Data, read and write.
The Classes
used ...
System.IO, System.Xml, DataSet, Label, TextBox and Button
- Name -
Xml-Book.
- Database
Software, use XML classes
- Developed with
Microsoft Visual Basic .Net.
- Functions ...
- Use ReadXml class
to read
the XML
document
- Use
WriteXml class
to write
the XML document
|
- Requirements:
Microsoft Visual Studio .Net.
If you want to run it:
- Remove the
read-only attribute of all files in the
folder - C:\Xml-Book
- Run the
Microsoft Visual Studio .Net.
- From
File menu, choose Open,
choose Project ..., the
Open Project dialog box appears,
select the project file Xml-Book.vbproj -
(C:\Xml-Book\Xml-Book.vbproj)
and then click Open.
- From
Debug menu, choose and click
Start.
|
Project - Xml-Book and those items
|
1. |
Create new Visual Basic project
Run Microsoft
Visual Studio .Net.
On the File menu , click New, and then click
Project .... The New Project dialog box appears.
- In the Project
Types pane, select the Visual Basic Projects
folder.
- In the
Templates pane, select Windows Application icon
(project type). A message appears indicating the type of
project you are going to create - (A project for creating an
application with a Windows user interface).
- In
the Name box,
type Xml-Book .
- In
the Location box, type C:\
- Click OK.
The Windows Application create all necessary
files and opens the Microsoft Visual Basic .Net project - Xml-Book. |
2. |
Rename Form - Form1.vb created
- On the View
menu , click Solution Explorer
- In Solution
Explorer, expand the Xml-Book project node and
select Form1.vb
- In Properties
windows, Type the new File Name
|
Old |
New |
Filename : |
Form1.vb |
BookForm.vb |
|
|
3. |
Create XML File - XMLFile1.xml
- On the View menu ,
click Solution Explorer
- In Solution Explorer,
right click the Xml-Book, select Add on the shortcut
menu to open other shortcut menu, click Add New Item, the Add
New Item - Xml-Book dialog box appears.
- In the Add New Item -Xml-Book
dialog box:
- In the Categories
pane, select Local Project Items
- In the Templates
pane, select XML File . A message appears - (A blank
XML File).
- In the Name
box, type XMLFile1.xml
- Click Open
New blank XML File
created ...
|
4. |
Create DataSet - Dataset1.xsd
- On the View menu ,
click Solution Explorer
- In Solution Explorer,
right click the Xml-Book, select Add on the shortcut
menu to open other shortcut menu, click Add New Item, the Add
New Item - Xml-Book dialog box appears.
- In the Add New Item -Xml-Book
dialog box:
- In the Categories
pane, select Local Project Items
- In the Templates
pane, select DataSet . A message appears - (A file
creating an XML schema with DataSet classes).
- In the Name
box, type Dataset1.xsd
- Click Open
New blank DataSet
created ...
|
5. |
Add 4 Element items to the dataSet
- Structure of the Record
book and the Fields included to be added to the DataSet
- From the ToolBox /XML
Schema, add 4 Element items to the DataSet designer
surface .
The DataSet design:
|
|
The Form design
.. |
 |
|
The View design
The Form Properties ...
|
Old
|
New |
Name:
FormBorderStyle
Icon:
Menu:
Size:
StartPosition:
Text:
Window State: |
Form1
Sizable
Icon
(none)
300,300
WindowsDefaultLocation
Form1
Normal |
BookForm
FixedSingle
Icon
(none)
536,250
CenterScreen
Main Form ...
Normal |
|
|
|
Add Dataset control from the
Toolbox/Data,
from the Add
Dataset dialog box appears,
select the Typed dataset radio button,
from the Name combobox select Xml_Book.Dataset1
and then click Ok.
New DataSet control - Dataset11
to be added to the Form BookForm.vb
From the
Toolbox/Windows Form add the following controls
Tree
TextBox
control, five Label controls
and eight Button controls.
The TextBox controls properties
|
TextBox Name |
DataBindings.text |
1. |
editBookid |
Dataset11 - book.Bookid |
2. |
editAuthor |
Dataset11 - book.Author |
3. |
editTitle |
Dataset11 - book.Title |
|
The Form functions:
- Navigate functions, read
data from the XML File by record
- Add function, Add new
record to the XML File.
- Update function, write the
added record to the XML File.
- Delete function, delete
the current XML record.
- Done function, unload the
application
|
The Functions used to read and write XML Data:
- ReadXml - Read the XML document
- WriteXml - Write the XML document
The Code ...
Imports
System.IO
Imports System.Xml
Public
Class
BookForm
Inherits
System.Windows.Forms.Form
Windows Form Designer generated code
|
' Load
the Data
Private
Sub
BookForm_Load(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
MyBase.Load
Try
Dim
Xmlfile As
String
= "C:\Xml-Book\XMLFile1.xml"
' Create
new FileStream to read schema with.
Dim
fsReadXml As
New
System.IO.FileStream(Xmlfile,
System.IO.FileMode.Open)
' Create
an XmlTextReader to read the file.
Dim
myXmlReader
As
New
System.Xml.XmlTextReader(fsReadXml)
' Read
the XML document into the DataSet.
Me.Dataset11.ReadXml(myXmlReader)
' Close
the XmlTextReader
myXmlReader.Close()
Catch
eLoad As
System.Exception
'Display
error message
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End
Try
Me.Dataset11_PositionChanged()
End
Sub
Private
Sub
Dataset11_PositionChanged()
Me.lblNavLocation.Text
= (((Me.BindingContext(Dataset11,
"book").Position + 1).ToString + " of ") _
+
Me.BindingContext(Dataset11,
"book").Count.ToString)
'
Navigation controls enabled(True or False
If
Me.BindingContext(Dataset11,
"book").Position = 0
Then
NavigationFP( False)
NavigationNL( True)
ElseIf
Me.BindingContext(Dataset11,
"book").Position =
Me.BindingContext(Dataset11,
"book").Count - 1
Then
NavigationFP( True)
NavigationNL( False)
Else
NavigationFP( True)
NavigationNL( True)
End
If
End
Sub
Private
Sub
NavigationFP(ByVal
bval As
Boolean)
Me.btnNavFirst.Enabled
= bval
Me.btnNavPrev.Enabled
= bval
End
Sub
Private
Sub
NavigationNL(ByVal
bval As
Boolean)
Me.btnNavNext.Enabled
= bval
Me.btnLast.Enabled
= bval
End
Sub
'
Navigate to the First Record
Private
Sub
btnNavFirst_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnNavFirst.Click
Me.BindingContext(Dataset11,
"book").Position = 0
Me.Dataset11_PositionChanged()
End
Sub
'
Navigate to The Previous Record
Private
Sub
btnNavPrev_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnNavPrev.Click
Me.BindingContext(Dataset11,
"book").Position = (Me.BindingContext(Dataset11,
"book").Position - 1)
Me.Dataset11_PositionChanged()
End
Sub
'
Navigate to The Next Record
Private
Sub
btnNavNext_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnNavNext.Click
Me.BindingContext(Dataset11,
"book").Position = (Me.BindingContext(Dataset11,
"book").Position + 1)
Me.Dataset11_PositionChanged()
End
Sub
'
Navigate to The Last Record
Private
Sub
btnLast_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnLast.Click
Me.BindingContext(Dataset11,
"book").Position = (Me.Dataset11.Tables("book").Rows.Count
- 1)
Me.Dataset11_PositionChanged()
End
Sub
'
Delete current Record
Private
Sub
btnDelete_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnDelete.Click
If
(Me.BindingContext(Dataset11,
"book").Count > 0)
Then
Me.BindingContext(Dataset11,
"book").RemoveAt(Me.BindingContext(Dataset11,
"book").Position)
Me.Dataset11_PositionChanged()
End
If
End
Sub
'
Add new Record
Private
Sub
btnAdd_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnAdd.Click
Try
'Clear
out the current edits
Me.BindingContext(Dataset11,
"book").EndCurrentEdit()
Me.BindingContext(Dataset11,
"book").AddNew()
Catch
eEndEdit As
System.Exception
System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
End
Try
Me.Dataset11_PositionChanged()
Me.btnUpdate_Click(Me.btnUpdate,
System.EventArgs.Empty)
End
Sub
'
Update/Save the new record added
Private
Sub
btnUpdate_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btnUpdate.Click
Try
'Attempt
to update the datasource.
Me.UpdateDataSet()
Catch
eUpdate As
System.Exception
'Add your
error handling code here.
'Display
error message, if any.
System.Windows.Forms.MessageBox.Show(eUpdate.Message)
End
Try
Me.Dataset11_PositionChanged()
End
Sub
Public
Sub
UpdateDataSet()
'Create a
new dataset to hold the changes that have been
made to the main dataset.
Dim
objDataSetChanges
As
Xml_Book.Dataset1 =
New
Xml_Book.Dataset1
Dim
objDataSetUpdated
As
System.Data.DataSet =
New
Xml_Book.Dataset1
'Stop any
current edits.
Me.BindingContext(Dataset11,
"book").EndCurrentEdit()
'Get the
changes that have been made to the main dataset.
objDataSetChanges =
CType(Dataset11.GetChanges,
Xml_Book.Dataset1)
'Check to
see if any changes have been made.
If
(Not
(objDataSetChanges)
Is
Nothing)
Then
Try
'There
are changes that need to be made, so attempt to
update the datasource by
'calling
the update method and passing the dataset and any
parameters.
objDataSetUpdated =
Me.Dataset11.Clone
Catch
eUpdate As
System.Exception
'Add your
error handling code here.
Throw
eUpdate
End
Try
'Add your
code to check the returned dataset for any errors
that may have been
'pushed
into the row object's error.
Try
Dataset11.Merge(objDataSetUpdated)
Catch
eUpdateMerge
As
System.Exception
'Add
exception handling code here
Throw
eUpdateMerge
End
Try
'Write
the XML
Dataset11.WriteXml("c:\Xml-Book\XMLFile1.xml",
XmlWriteMode.DiffGram)
Dataset11.AcceptChanges()
End
If
End
Sub
'
Unload the current application
Private
Sub
btndone_Click(ByVal
sender As
System.Object,
ByVal
e As
System.EventArgs)
Handles
btndone.Click
Application.Exit()
End
Sub
End
Class |
| | |
|
|