By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Using a form to add records to XML document

P: n/a
Hi,

I haveto use VB to create a form that reads an exisiting XML fileand then allows updates via the VB form.

My problem is that I was able to get VB to read a simple XML file(people.XML), but I'm having problems figuring out how to get VBto read a more complex XML file (people2.xml) and then useadditional text boxes on the same form to add more familymembers. Each family can have from 1 to 5 members.

I've pasted the working code below that read people.xml
but would appreciate any assistance regarding people2.xml

Thanks much,
kyma
people.xml --- SIMPLE XML

<?xml version="1.0"?>
<FamilyList xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Families>
<Family>
<LastName>Flanders</LastName>
<FirstName>Ned</FirstName>
<Age>60</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Maude</FirstName>
<Age>34</Age>
<Gender>Female</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Rod</FirstName>
<Age>10</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Todd</FirstName>
<Age>8</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Timothy</FirstName>
<Age>37</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Helen</FirstName>
<Age>36</Age>
<Gender>Female</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Jessica</FirstName>
<Age>10</Age>
<Gender>Female</Gender>
</Family>
</Families>
</FamilyList>

people2.xml --- COMPLEX XML

<?xml version="1.0"?>
<FamilyList xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Families>
<Family>
<Member>
<LastName>Flanders</LastName>
<FirstName>Ned</FirstName>
<Age>60</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Maude</FirstName>
<Age>34</Age>
<Gender>Female</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Rod</FirstName>
<Age>10</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Todd</FirstName>
<Age>8</Age>
<Gender>Male</Gender>
</Member>
</Family>
<Family>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Timothy</FirstName>
<Age>37</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Helen</FirstName>
<Age>36</Age>
<Gender>Female</Gender>
</Member>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Jessica</FirstName>
<Age>10</Age>
<Gender>Female</Gender>
</Member>
</Family>
</Families>
</FamilyList>

frmFamilyList --- THE FORM

Public Class frmFamilyList
Inherits System.Windows.Forms.Form

...

'Members
Public FamilyList As FamilyList
Private _currentFamilyIndex As Integer

'Current Family
ReadOnly Property CurrentFamily() As Family
Get
Return FamilyList.Items(CurrentFamilyIndex - 1)
End Get
End Property
'Current Address Index
Property CurrentFamilyIndex() As Integer
Get
Return _currentFamilyIndex
End Get
Set(ByVal Value As Integer)
'Set family
_currentFamilyIndex = Value

'Update display
PopulateFormFromFamily(CurrentFamily)

'Record number information
lblRecordNumber.Text = _currentFamilyIndex & " of " &FamilyList.Items.Count

End Set
End Property

'Data location
Public ReadOnly Property DataFilename() As String
Get
'Working folder
Dim folder As String
folder = Environment.CurrentDirectory

'Return folder with name of file
Return folder & "\people.xml"

End Get
End Property

'Populate Family from form fields
Public Sub PopulateFamilyFromForm(ByVal family As Family)
'Copy values
family.LastName = txtLastName.text
family.FirstName = txtFirstName.text
family.Age = txtAge.text
family.Gender = txtGender.text

End Sub

'Populate form from Family
Public Sub PopulateFormFromFamily(ByVal family As Family)
'Copy values
txtLastName.Text = family.LastName
txtFirstName.Text = family.FirstName
txtAge.Text = family.Age
txtGender.Text = family.Gender

End Sub
Private Sub frmFamilyList_Load(ByVal sender As Object, ByVale As System.EventArgs) Handles MyBase.Load
'load family list
FamilyList = SerializableData.load(DataFilename,GetType(FamilyL ist))

'Make sure family list contains items
If FamilyList.Items.Count = 0 ThenFamilyList.AddFamily()

'Select first item
CurrentFamilyIndex = 1

End Sub
Private Sub frmFamilyList_Closing(ByVal sender As Object,ByVal e As System.ComponentModel.CancelEventArgs) HandlesMyBase.Closing
'Save changes as form closes
UpdateCurrentFamily()
SaveChanges()

End Sub

'Save to XML file
Public Sub SaveChanges()
FamilyList.Save(DataFilename)
End Sub

'Ensure file has current values entered into form
Private Sub UpdateCurrentFamily()
PopulateFamilyFromForm(CurrentFamily)

End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVale As System.EventArgs) Handles btnAdd.Click
AddNewFamily()
End Sub
Public Function AddNewFamily() As Family

'Save current family
UpdateCurrentFamily()

'Create new family
Dim newFamily As Family = FamilyList.AddFamily

'Update display
CurrentFamilyIndex = FamilyList.Items.Count

'Return
Return newFamily

End Class

SerializableData --- CLASS SAVING/LOADING

Imports System.IO
Imports System.Xml.Serialization

Public Class SerializableData

'Save object to file
Public Function Save(ByVal filename As String)

'Temporary
Dim tempFilename As String
tempFilename = filename & ".tmp"

'Verify file exists
Dim tempFileInfo As New FileInfo(tempFilename)
If tempFileInfo.Exists = True Then tempFileInfo.Delete()

'Open file
Dim stream As New FileStream(tempFilename,FileMode.Create)

'Save object
Save(stream)

'Close file
stream.Close()

'Remove existing file and rename temp file
tempFileInfo.CopyTo(filename, True)
tempFileInfo.Delete()

End Function

'Perform save
Public Function Save(ByVal stream As Stream)

'Create serializer
Dim serializer As New XmlSerializer(Me.GetType)

'Save file
serializer.Serialize(stream, Me)

End Function

'Load from file
Public Shared Function load(ByVal filename As String, ByValnewType As Type) As Object

'Verify file exists
Dim fileInfo As New FileInfo(filename)
If fileInfo.Exists = False Then

'Create and return blank version of object
Return System.Activator.CreateInstance(newType)

End If

'Open file
Dim stream As New FileStream(filename, FileMode.Open)

'Load object from stream
Dim newObject As Object = load(stream, newType)

'Close stream
stream.Close()

'Return
Return newObject

End Function
Public Shared Function Load(ByVal stream As Stream, ByValnewType As Type) As Object

'Create serializer and load object
Dim serializer As New XmlSerializer(newType)
Dim newObject As Object = serializer.Deserialize(stream)

'Return
Return newObject

End Function
End Class


FamilyList --- CLASS

Imports System.Xml.Serialization

Public Class FamilyList
Inherits SerializableData

'Members
<XmlIgnore()> Public Items As New ArrayList

'Add new family
Public Function AddFamily() As Family

'Declare
Dim newFamily As New Family

'Add
Items.Add(newFamily)

'Return
Return newFamily

End Function
'Works with items collection as an array
Public Property Families() As Family()
Get
'New Array
Dim familyArray(Items.Count - 1) As Family
Items.CopyTo(familyArray)
Return familyArray
End Get
Set(ByVal Value As Family())
'Reset array list
Items.Clear()
'Any values?
If Not Value Is Nothing Then
'process array and populate items
Dim family As Family
For Each family In Value
Items.Add(family)
Next
End If
End Set
End Property
End Class

Family --- CLASS

Public Class Family
Inherits SerializableData

'Members
Public LastName As String
Public FirstName As String
Public Age As String
Public Gender As String

End Class

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>kftW9oBAvU60IVqDullVCw==</Id>
Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"VB" is not as easy as other languages.

Is was just more so previewed and as such a token so percieved.

As was freedom and liberty in 1776.

------------
In reality, do you want more than one click or key pressed to
compile and run your changes at a client presented.
And look at the galore of umpteen forms and files and permissions
licenses that come along.

===========
In short. You are OUT.



"kyma via .NET 247" <an*******@dotnet247.com> wrote in message
news:eU**************@tk2msftngp13.phx.gbl...
Hi,

I haveto use VB to create a form that reads an exisiting XML file and then
allows updates via the VB form.

My problem is that I was able to get VB to read a simple XML file
(people.XML), but I'm having problems figuring out how to get VB to read a
more complex XML file (people2.xml) and then use additional text boxes on
the same form to add more family members. Each family can have from 1 to 5
members.

I've pasted the working code below that read people.xml
but would appreciate any assistance regarding people2.xml

Thanks much,
kyma
people.xml --- SIMPLE XML

<?xml version="1.0"?>
<FamilyList xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Families>
<Family>
<LastName>Flanders</LastName>
<FirstName>Ned</FirstName>
<Age>60</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Maude</FirstName>
<Age>34</Age>
<Gender>Female</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Rod</FirstName>
<Age>10</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Flanders</LastName>
<FirstName>Todd</FirstName>
<Age>8</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Timothy</FirstName>
<Age>37</Age>
<Gender>Male</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Helen</FirstName>
<Age>36</Age>
<Gender>Female</Gender>
</Family>
<Family>
<LastName>Lovejoy</LastName>
<FirstName>Jessica</FirstName>
<Age>10</Age>
<Gender>Female</Gender>
</Family>
</Families>
</FamilyList>

people2.xml --- COMPLEX XML

<?xml version="1.0"?>
<FamilyList xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Families>
<Family>
<Member>
<LastName>Flanders</LastName>
<FirstName>Ned</FirstName>
<Age>60</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Maude</FirstName>
<Age>34</Age>
<Gender>Female</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Rod</FirstName>
<Age>10</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Flanders</LastName>
<FirstName>Todd</FirstName>
<Age>8</Age>
<Gender>Male</Gender>
</Member>
</Family>
<Family>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Timothy</FirstName>
<Age>37</Age>
<Gender>Male</Gender>
</Member>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Helen</FirstName>
<Age>36</Age>
<Gender>Female</Gender>
</Member>
<Member>
<LastName>Lovejoy</LastName>
<FirstName>Jessica</FirstName>
<Age>10</Age>
<Gender>Female</Gender>
</Member>
</Family>
</Families>
</FamilyList>

frmFamilyList --- THE FORM

Public Class frmFamilyList
Inherits System.Windows.Forms.Form

....

'Members
Public FamilyList As FamilyList
Private _currentFamilyIndex As Integer

'Current Family
ReadOnly Property CurrentFamily() As Family
Get
Return FamilyList.Items(CurrentFamilyIndex - 1)
End Get
End Property
'Current Address Index
Property CurrentFamilyIndex() As Integer
Get
Return _currentFamilyIndex
End Get
Set(ByVal Value As Integer)
'Set family
_currentFamilyIndex = Value

'Update display
PopulateFormFromFamily(CurrentFamily)

'Record number information
lblRecordNumber.Text = _currentFamilyIndex & " of " &
FamilyList.Items.Count

End Set
End Property

'Data location
Public ReadOnly Property DataFilename() As String
Get
'Working folder
Dim folder As String
folder = Environment.CurrentDirectory

'Return folder with name of file
Return folder & "\people.xml"

End Get
End Property

'Populate Family from form fields
Public Sub PopulateFamilyFromForm(ByVal family As Family)
'Copy values
family.LastName = txtLastName.text
family.FirstName = txtFirstName.text
family.Age = txtAge.text
family.Gender = txtGender.text

End Sub

'Populate form from Family
Public Sub PopulateFormFromFamily(ByVal family As Family)
'Copy values
txtLastName.Text = family.LastName
txtFirstName.Text = family.FirstName
txtAge.Text = family.Age
txtGender.Text = family.Gender

End Sub
Private Sub frmFamilyList_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'load family list
FamilyList = SerializableData.load(DataFilename,
GetType(FamilyList))

'Make sure family list contains items
If FamilyList.Items.Count = 0 Then FamilyList.AddFamily()

'Select first item
CurrentFamilyIndex = 1

End Sub
Private Sub frmFamilyList_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'Save changes as form closes
UpdateCurrentFamily()
SaveChanges()

End Sub

'Save to XML file
Public Sub SaveChanges()
FamilyList.Save(DataFilename)
End Sub

'Ensure file has current values entered into form
Private Sub UpdateCurrentFamily()
PopulateFamilyFromForm(CurrentFamily)

End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
AddNewFamily()
End Sub
Public Function AddNewFamily() As Family

'Save current family
UpdateCurrentFamily()

'Create new family
Dim newFamily As Family = FamilyList.AddFamily

'Update display
CurrentFamilyIndex = FamilyList.Items.Count

'Return
Return newFamily

End Class

SerializableData --- CLASS SAVING/LOADING

Imports System.IO
Imports System.Xml.Serialization

Public Class SerializableData

'Save object to file
Public Function Save(ByVal filename As String)

'Temporary
Dim tempFilename As String
tempFilename = filename & ".tmp"

'Verify file exists
Dim tempFileInfo As New FileInfo(tempFilename)
If tempFileInfo.Exists = True Then tempFileInfo.Delete()

'Open file
Dim stream As New FileStream(tempFilename, FileMode.Create)

'Save object
Save(stream)

'Close file
stream.Close()

'Remove existing file and rename temp file
tempFileInfo.CopyTo(filename, True)
tempFileInfo.Delete()

End Function

'Perform save
Public Function Save(ByVal stream As Stream)

'Create serializer
Dim serializer As New XmlSerializer(Me.GetType)

'Save file
serializer.Serialize(stream, Me)

End Function

'Load from file
Public Shared Function load(ByVal filename As String, ByVal newType As
Type) As Object

'Verify file exists
Dim fileInfo As New FileInfo(filename)
If fileInfo.Exists = False Then

'Create and return blank version of object
Return System.Activator.CreateInstance(newType)

End If

'Open file
Dim stream As New FileStream(filename, FileMode.Open)

'Load object from stream
Dim newObject As Object = load(stream, newType)

'Close stream
stream.Close()

'Return
Return newObject

End Function
Public Shared Function Load(ByVal stream As Stream, ByVal newType As
Type) As Object

'Create serializer and load object
Dim serializer As New XmlSerializer(newType)
Dim newObject As Object = serializer.Deserialize(stream)

'Return
Return newObject

End Function
End Class


FamilyList --- CLASS

Imports System.Xml.Serialization

Public Class FamilyList
Inherits SerializableData

'Members
<XmlIgnore()> Public Items As New ArrayList

'Add new family
Public Function AddFamily() As Family

'Declare
Dim newFamily As New Family

'Add
Items.Add(newFamily)

'Return
Return newFamily

End Function
'Works with items collection as an array
Public Property Families() As Family()
Get
'New Array
Dim familyArray(Items.Count - 1) As Family
Items.CopyTo(familyArray)
Return familyArray
End Get
Set(ByVal Value As Family())
'Reset array list
Items.Clear()
'Any values?
If Not Value Is Nothing Then
'process array and populate items
Dim family As Family
For Each family In Value
Items.Add(family)
Next
End If
End Set
End Property
End Class

Family --- CLASS

Public Class Family
Inherits SerializableData

'Members
Public LastName As String
Public FirstName As String
Public Age As String
Public Gender As String

End Class

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>kftW9oBAvU60IVqDullVCw==</Id>

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.