![]() |
Operations on XML Elements |
|
Fundamental Operations |
|
Introduction |
|
So far, to create an XML element, we were directly typing in a file. When such a file had been created and saved, it was ready to be processed. Here is an example of a file named videos.xml: <?xml version="1.0" encoding="utf-8" ?> <videos> <title>The Distinguished Gentleman</title> </videos> In some applications, you will want the user to provide you with the necessary value(s) to create an element. Fortunately, the XmlDocument, the XmlNode, and the XmlElement classes provide all the necessary properties and methods to perform the routine operations of an XML file, an element, or a node. The operations include locating a node, adding a new element, or deleting a node. |
|
Before performing an operation, you will usually need to decide in what section of the file you want the action to be applied. As it happens, you have on the root node, on a particular node inside the document, on the parent of a node, on a sibling of a node, etc. To get to a node, you will usually first get a reference to its XmlElement object. To do this, you can declare an XmlElement variable and initialize it with that reference. |
|
|
| (Name) | Text | TextAlign | Width |
| colAccountNumber | Account # | 65 | |
| colFullName | Full Name | 120 | |
| colMaritalStatus | Marital Status | 85 | |
| colPhoneNumber | Phone # | Center | 85 |
![]() |
||||||||||||||||
|
Imports System.IO Imports System.Xml Public Class Tenants End Class |
Private Sub ShowTenants()
Dim Filename As String = "C:\Solas Property Rental\tenants.xml"
Dim docTenants As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
lvwTenants.Items.Clear()
docTenants.Load(Filename)
Dim TenantElement As XmlElement = docTenants.DocumentElement
Dim ListOfTenants As XmlNodeList = TenantElement.ChildNodes
For Each Node As XmlNode In ListOfTenants
Dim lviTenant As ListViewItem = New ListViewItem(Node.FirstChild.InnerText) ' Account Number
lviTenant.SubItems.Add(Node.FirstChild.NextSibling.InnerText) ' Full Name
lviTenant.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.InnerText) ' Phone Number
lviTenant.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText) ' Marital Status
lvwTenants.Items.Add(lviTenant)
Next
End If
End Sub
|
Private Sub Tenants_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
ShowTenants()
End Sub
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||
|
| (Name) | Text | TextAlign | Width |
| colPropertyCode | Prop Code | 65 | |
| colPropertyType | Property Type | 85 | |
| colBedrooms | Bedrooms | Right | 65 |
| colBathrooms | Bathrooms | Right | 65 |
| colMonthlyRent | Monthly Rent | Right | 75 |
| colStatus | Status | 65 |
![]() |
||||||||||||||||
|
Imports System.IO Imports System.Xml Public Class RentalProperties End Class |
Private Sub ShowProperties()
Dim DOMProperties As XmlDocument = New XmlDocument
Dim Filename As String = "C:\Solas Property Rental\properties.xml"
If File.Exists(Filename) Then
lvwProperties.Items.Clear()
DOMProperties.Load(Filename)
Dim RootProperty As XmlElement = DOMProperties.DocumentElement
Dim ListOfProperties As XmlNodeList = RootProperty.ChildNodes
For Each Node As XmlNode In ListOfProperties
Dim lviProperty As ListViewItem = New ListViewItem(Node.FirstChild.InnerText) ' Property Code
lviProperty.SubItems.Add(Node.FirstChild.NextSibling.InnerText) ' Property Type
lviProperty.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.InnerText) ' Bedrooms
lviProperty.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText) ' Bathrooms
lviProperty.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Monthly Rent
lviProperty.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Status
lvwProperties.Items.Add(lviProperty)
Next
End If
End Sub
|
Private Sub RentalProperties_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
ShowProperties()
End Sub
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Imports System.IO Imports System.Xml Public Class RentalAllocation End Class |
Private Sub txtTenantAcntNbr_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtTenantAcntNbr.Leave
Dim Filename As String = "C:\Solas Property Rental\tenants.xml"
Dim docTenants As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
docTenants.Load(Filename)
Dim TenantElement As XmlElement = docTenants.DocumentElement
Dim ListOfTenants As XmlNodeList = TenantElement.ChildNodes
Dim TenantFound As Boolean = False
For i = 0 To ListOfTenants.Count - 1
Dim Node As XmlNode = ListOfTenants(i)
If Node.FirstChild.InnerText = txtTenantAcntNbr.Text Then
TenantFound = True
txtTenantName.Text = Node.FirstChild.NextSibling.InnerText
txtMaritalStatus.Text = Node.FirstChild.NextSibling.NextSibling.InnerText
End If
Next
If TenantFound = False Then
MsgBox("There is no tenant with that account number")
txtTenantAcntNbr.Text = ""
End If
Else
MsgBox("There is no list of tenants to check.")
End If
End Sub
|
Private Sub txtPropertyCode_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtPropertyCode.Leave
Dim Filename As String = "C:\Solas Property Rental\properties.xml"
Dim DOMProperties As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMProperties.Load(Filename)
Dim PropertyElement As XmlElement = DOMProperties.DocumentElement
Dim ListOfProperties As XmlNodeList = PropertyElement.ChildNodes
Dim PropertyFound As Boolean = False
For i = 0 To ListOfProperties.Count - 1
Dim Node As XmlNode = ListOfProperties(i)
If Node.FirstChild.InnerText = txtPropertyCode.Text Then
PropertyFound = True
txtPropertyType.Text = Node.FirstChild.NextSibling.InnerText
txtMonthlyRent.Text = Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText
End If
Next
If PropertyFound = False Then
MsgBox("There is no Property with that code")
txtPropertyType.Text = ""
End If
Else
MsgBox("There is no list of properties to check.")
End If
End Sub
|
| (Name) | Text | TextAlign | Width |
| colAllocationCode | Alloc Code | 65 | |
| colDateAllocated | Date Allocated | Center | 85 |
| colTenantAccount | Tenant # | Center | 65 |
| colTenantName | Tenant Name | 100 | |
| colPropertyCode | Prop Code | Center | 65 |
| colPropertyType | Prop Type | 75 | |
| colContractLength | Contract Length | 88 | |
| colRentStartDate | Rent Start Date | Center | 88 |
| colMonthlyRent | Monthly Rent | Right | 76 |
Imports System.IO Imports System.Xml Public Class RentalAllocations End Class |
Private Sub ShowRentalAllocations()
Dim Filename As String = "C:\Solas Property Rental\contracts.xml"
Dim docAllocations As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
lvwAllocations.Items.Clear()
docAllocations.Load(Filename)
Dim AllocationElement As XmlElement = docAllocations.DocumentElement
Dim ListOfAllocations As XmlNodeList = AllocationElement.ChildNodes
For Each Node As XmlNode In ListOfAllocations
Dim lviAllocation As ListViewItem = New ListViewItem(Node.FirstChild.InnerText) ' Allocation Code
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.InnerText) ' Date Allocated
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.InnerText) ' Tenant Account Number
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText) ' Tenant Name
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Property Code
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Property Type
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Contract Length
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Rent Start Date
lviAllocation.SubItems.Add(Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Monthly Rent
lvwAllocations.Items.Add(lviAllocation)
Next
End If
End Sub
|
Private Sub RentalAllocations_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
ShowRentalAllocations()
End Sub
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Imports System.IO Imports System.Xml Public Class RentPayment End Class |
Private Sub txtAllocationCode_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtAllocationCode.Leave
Dim Filename As String = "C:\Solas Property Rental\contracts.xml"
Dim docAllocations As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
docAllocations.Load(Filename)
Dim AllocationElement As XmlElement = docAllocations.DocumentElement
Dim ListOfAllocations As XmlNodeList = AllocationElement.ChildNodes
Dim ContractFound As Boolean = False
For i = 0 To ListOfAllocations.Count - 1
Dim Node As XmlNode = ListOfAllocations(i)
If Node.FirstChild.InnerText = txtAllocationCode.Text Then
ContractFound = True
txtTenantAcntNber.Text = Node.FirstChild.NextSibling.NextSibling.InnerText
txtTenantName.Text = Node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText
txtPropertyCode.Text = Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText
txtPropertyType.Text = Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText
txtAmountReceived.Text = Node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText
End If
Next
If ContractFound = False Then
MsgBox("There is no rental contract with that number")
txtTenantAcntNber.Text = ""
End If
Else
MsgBox("There is no list of rental contracts to check.")
End If
End Sub
|
| (Name) | Text | TextAlign | Width |
| colReceiptNumber | Receipt # | Right | |
| colDateReceived | Date Received | Center | 85 |
| colAllocationCode | Alloc Code | Center | 65 |
| colTenantAccount | Tenant # | Center | 65 |
| colTenantName | Tenant Name | 100 | |
| colPropertyCode | Prop Code | Center | 65 |
| colPropertyType | Prop Type | 75 | |
| colPaymentFor | Payment For | 88 | |
| colAmountReceived | Amount | Right | 50 |
![]() |
||||||||||||||||
|
Imports System.IO Imports System.Xml Public Class RentPayments End Class |
Private Sub ShowRentPayments()
Dim Filename As String = "C:\Solas Property Rental\payments.xml"
Dim DocumentPayments As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
lvwRentPayments.Items.Clear()
DocumentPayments.Load(Filename)
Dim AllocationElement As XmlElement = DocumentPayments.DocumentElement
Dim ListOfAllocations As XmlNodeList = AllocationElement.ChildNodes
For Each Node As XmlNode In ListOfAllocations
Dim lviPayment As ListViewItem = New ListViewItem(node.FirstChild.InnerText) ' Receipt Number
lviPayment.SubItems.Add(node.FirstChild.NextSibling.InnerText) ' Date Received
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.InnerText) ' Allocation Code
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText) ' Tenant Account Number
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Tenant Name
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Property Code
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Property Type
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Payment For
lviPayment.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText) ' Amount
lvwRentPayments.Items.Add(lviPayment)
Next
End If
End Sub
|
Private Sub RentPayments_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
ShowRentPayments()
End Sub
|
![]() |
|||||||||||||||
|
Private Sub btnRentPayments_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnRentPayments.Click
Dim frmPayment As RentPayments = New RentPayments
frmPayment.Show()
nd Sub
|
Private Sub btnRentalAllocations_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnRentalAllocations.Click
Dim frmAllocations As RentalAllocations = New RentalAllocations
frmAllocations.ShowDialog()
End Sub
|
Private Sub btnTenants_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnTenants.Click
Dim frmTenants As Tenants = New Tenants
frmTenants.ShowDialog()
End Sub
|
Private Sub btnRentalProperties_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnRentalProperties.Click
Dim frmProperties As RentalProperties = New RentalProperties
frmProperties.ShowDialog()
End Sub
|
Private Sub btnClose_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click
End
End Sub
|
To assist with in programmatically creating a new element, the XmlDocument class provides the CreateElement() method that is overloaded with three versions. One of the versions uses the following syntax: Public Function CreateElement(name As String) As XmlElement Using this method, to create a new element, call it and pass it the name of the element. For example, imagine you want to add a new Title element to the above file. You would start with code as follows: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim NewElement As XmlElement = DOMDocument.CreateElement("Title")
End If
End Sub
In order to add the new element to the file, you must specify its position in the tree: whether it would be the first or the last node, whether you want to position it before or after a node of your choice. For example, if you want to add a new Title element to the above file, it would be considered a child of the root, that is, a child of the XmlDocument.DocumentElement property. In the previous lesson, we learned how to get a reference to the root node. To support the positions of existing nodes, the XmlNode class, which is the ancestor of all XML nodes of the .NET Framework, provides various appropriate methods. One of these methods is AppendChild(), which is used to add an element as the last child of its parent. The syntax of the XmlNode.AppendChild() method is: Public Overridable Function AppendChild(newChild As XmlNode) As XmlNode When calling this method, pass the XmlNode object you had previous created. After adding the node, if you want the file to keep it, you should save it. Here is an example: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim RootElement As XmlElement = DOMDocument.DocumentElement
Dim NewElement As XmlElement = DOMDocument.CreateElement("title")
RootElement.AppendChild(NewElement)
DOMDocument.Save(Filename)
End If
End Sub
This would produce: <?xml version="1.0" encoding="utf-8"?> <videos> <title>The Distinguished Gentleman</title> <title /> </videos> Notice that the newly added element is empty.
Suppose you have the following videos.xml file: <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
Imagine that you want to add a video element. You have a choice of adding one, more than one, or all child elements of the video node. To perform this operation, one solution you can use is to "build" all child elements of the video element, then add the node as a whole. In the previous lesson, we saw that the XmlNode.InnerXml property comprises a node, its markup, its children and their markup. This means that you can create the child node(s) with its (their) markup(s) as a string and assign that string to an XmlNode.InnerXml string. To do this, you would need the set version of the InnerXml property. It is declared as follows: Public Overridable Property InnerXml As String Here is an example that adds a complete new video node to the above XML file: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim Element As XmlElement = DOMDocument.CreateElement("video")
Dim strNewvideo As String = "<title>Other People's Money</title>" & _
"<director>Alan Brunstein</director>" & _
"<length>114 Minutes</length>" & _
"<format>VHS</format>" & _
"<rating>PG-13</rating>"
Element.InnerXml = strNewvideo
DOMDocument.DocumentElement.AppendChild(Element)
DOMDocument.Save(Filename)
End If
End Sub
This would produce: <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Mins</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
<video>
<title>Other People's Money</title>
<director>Alan Brunstein</director>
<length>114 Minutes</length>
<format>VHS</format>
<rating>PG-13</rating>
</video>
</videos>
Consider the following XML file named videos.xml: <?xml version="1.0" encoding="utf-8" ?> <videos> <title>The Distinguished Gentleman</title> </videos> If you want the element to have a value, the XmlDocument class provides the CreateTextNode() method. This method returns an XmlText value. The syntax of this method is: Public Overridable Function CreateTextNode(text As String) As XmlText This method takes as argument the string that would constitute the value of the element. Before calling it, you should have used the XmlNode.AppendChild() method to create a node. Calling this method on the LastChild node of the one that called the AppendChild() would specify the value of the new node. Here is an example: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim RootElement As XmlElement = DOMDocument.DocumentElement
Dim ElementTitle As XmlElement = DOMDocument.CreateElement("title")
Dim TextVideo As XmlText = DOMDocument.CreateTextNode("Basic Instinct")
RootElement.AppendChild(ElementTitle)
RootElement.LastChild.AppendChild(TextVideo)
DOMDocument.Save(Filename)
End If
End Sub
This would produce: <?xml version="1.0" encoding="utf-8"?> <videos> <title>The Distinguished Gentleman</title> <title>Basic Instinct</title> </videos> The combination of calls to XmlDocument.CreateElement() and XmlDocument.CreateTextNode() allow you to create a new element that has a value. Consider the following XML file named "videos.xml": <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
</video>
<video>
<title>Basic Instinct</title>
</video>
</videos>
Notice that the root, videos, has a repetitive child named video. This video child has its own child named Title. Imagine that you want to add a new video node that has a child. To do this, first create an empty video node as a child of the root. We learned earlier how to do that: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim RootElement As XmlElement = DOMDocument.DocumentElement
Dim Element As XmlElement = DOMDocument.CreateElement("video")
RootElement.AppendChild(Element)
End If
End Sub
After creating the new child of the root, initialize the grand child with the desired name (the name does not have to be one of the existing names) and a value (which is optional if you do not want the new node to have a value). Once the new node is ready, add it as the last child of the root. If this new node has a value, add its XmlText object as the LastChild of the LastChild of the root. Here is an example of how you would do this: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim RootElement As XmlElement = DOMDocument.DocumentElement
Dim Element As XmlElement = DOMDocument.CreateElement("video")
RootElement.AppendChild(Element)
RootElement = DOMDocument.DocumentElement
Element = DOMDocument.CreateElement("title")
Dim TextVideo As XmlText = DOMDocument.CreateTextNode("Her Alibi")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
DOMDocument.Save(Filename)
End If
End Sub
This would produce: <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
</video>
<video>
<title>Basic Instinct</title>
</video>
<video>
<title>Her Alibi</title>
</video>
</videos>
Now consider the following file: <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
The root, videos, has a child named video. The video node has many child nodes. By now, we know how to add a child to the root. We also saw how to add a grand child with value to the root. To had many (grand) children to a node, first build the node, add it to the root, then continuously add the necessary nodes, one at a time, including its name and its optional value. This would be done as follows: Private Sub btnDocument_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
TenamHandles btnDocument.Click
Dim Filename As String = "videos.xml"
Dim DOMDocument As XmlDocument = New XmlDocument
If File.Exists(Filename) Then
DOMDocument.Load(Filename)
Dim RootElement As XmlElement = DOMDocument.DocumentElement
Dim Element As XmlElement = DOMDocument.CreateElement("video")
RootElement.AppendChild(Element)
RootElement = DOMDocument.DocumentElement
Element = DOMDocument.CreateElement("title")
Dim TextVideo As XmlText = DOMDocument.CreateTextNode("The Day After Tomorrow")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
Element = DOMDocument.CreateElement("director")
TextVideo = DOMDocument.CreateTextNode("Roland Emmerich")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
Element = DOMDocument.CreateElement("length")
TextVideo = DOMDocument.CreateTextNode("124 Minutes")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
Element = DOMDocument.CreateElement("format")
TextVideo = DOMDocument.CreateTextNode("DVD")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
Element = DOMDocument.CreateElement("rating")
TextVideo = DOMDocument.CreateTextNode("PG-13")
RootElement.LastChild.AppendChild(Element)
RootElement.LastChild.LastChild.AppendChild(TextVideo)
DOMDocument.Save(Filename)
End If
End Sub
This would produce: <?xml version="1.0" encoding="utf-8"?>
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
<video>
<title>The Day After Tomorrow</title>
<director>Roland Emmerich</director>
<length>124 Minutes</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
Using the same approach, you can add children to children of children, and so on. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Private Sub btnNewAllocation_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnNewAllocation.Click
Dim AllocationCode As String
Dim TenantAccountNumber As String
Dim TenantName As String
Dim MaritalStatus As String
Dim PropertyCode As String
Dim PropertyType As String
Dim ContractLength As String
Dim DateAllocated As DateTime
Dim RentStartDate As Date
Dim MonthlyRent As Double
Dim Editor As RentalAllocation = New RentalAllocation
Directory.CreateDirectory("C:\Solas Property Rental")
Dim Filename As String = "C:\Solas Property Rental\contracts.xml"
If Editor.ShowDialog() = DialogResult.OK Then
Dim DOMAllocation As XmlDocument = New XmlDocument
If Not File.Exists(Filename) Then
DOMAllocation.LoadXml( _
"<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<Allocations></Allocations>")
DOMAllocation.Save(Filename)
End If
DOMAllocation.Load(Filename)
Dim RootElement As XmlElement = DOMAllocation.DocumentElement
AllocationCode = Editor.txtAllocationCode.Text
DateAllocated = Editor.dtpDateAllocated.Value
TenantAccountNumber = Editor.txtTenantAcntNbr.Text
TenantName = Editor.txtTenantName.Text
MaritalStatus = Editor.txtMaritalStatus.Text
PropertyCode = Editor.txtPropertyCode.Text
PropertyType = Editor.txtPropertyType.Text
MonthlyRent = CDbl(Editor.txtMonthlyRent.Text)
ContractLength = Editor.cbxContractLengths.Text
RentStartDate = Editor.dtpRentStartDate.Value
Dim RootElement As XmlElement = DOMAllocation.DocumentElement
Dim AllocationElement As XmlElement = _
DOMAllocation.CreateElement("RentalAllocation")
RootElement.AppendChild(AllocationElement)
RootElement = DOMAllocation.DocumentElement
AllocationElement = DOMAllocation.CreateElement("AllocationCode")
Dim txtAllocation As XmlText = _
DOMAllocation.CreateTextNode(AllocationCode)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("DateAllocated")
txtAllocation = _
DOMAllocation.CreateTextNode(DateAllocated.ToString("d"))
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = _
DOMAllocation.CreateElement("TenantAccountNumber")
txtAllocation = _
DOMAllocation.CreateTextNode(TenantAccountNumber)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("TenantName")
txtAllocation = DOMAllocation.CreateTextNode(TenantName)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("MaritalStatus")
txtAllocation = DOMAllocation.CreateTextNode(MaritalStatus)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("PropertyCode")
txtAllocation = DOMAllocation.CreateTextNode(PropertyCode)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("PropertyType")
txtAllocation = DOMAllocation.CreateTextNode(PropertyType)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("ContractLength")
txtAllocation = DOMAllocation.CreateTextNode(ContractLength)
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("RentStartDate")
txtAllocation = _
DOMAllocation.CreateTextNode(RentStartDate.ToString("d"))
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
AllocationElement = DOMAllocation.CreateElement("MonthlyRent")
txtAllocation = _
DOMAllocation.CreateTextNode(FormatNumber(MonthlyRent))
RootElement.LastChild.AppendChild(AllocationElement)
RootElement.LastChild.LastChild.AppendChild(txtAllocation)
DOMAllocation.Save(Filename)
ShowRentalAllocations()
End If
End Sub
|
Private Sub btnClose_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click
Close()
End Sub
|
Private Sub btnNewPayment_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnNewPayment.Click
Dim ReceiptNumber As Integer
Dim AllocationCode As String
Dim TenantAccountNumber As String
Dim TenantName As String
Dim PaymentFor As String
Dim PropertyCode As String
Dim PropertyType As String
Dim DateReceived As DateTime
Dim AmountReceived As Double
Dim Editor As RentPayment = New RentPayment
Dim DOMPayment As XmlDocument = New XmlDocument
Directory.CreateDirectory("C:\Solas Property Rental")
Dim Filename As String = "C:\Solas Property Rental\payments.xml"
' If some payments were previously made
If File.Exists(Filename) Then
' Open the payments.xml file
DOMPayment.Load(Filename)
' Locate the root element
Dim AllocationElement As XmlElement = DOMPayment.DocumentElement
' Get a list of the child nodes
Dim ListOfAllocations As XmlNodeList = AllocationElement.ChildNodes
' Get the last receipt number
ReceiptNumber = _
CInt(ListOfAllocations(ListOfAllocations.Count _
- 1).FirstChild.InnerText) + 1
Editor.txtReceiptNumber.Text = ReceiptNumber.ToString()
Else
' If no payment has ever been made,
' create a new XML file for the payments
DOMPayment.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<RentPayments></RentPayments>")
' We will start the receipt numbers at 101
ReceiptNumber = 101
Editor.txtReceiptNumber.Text = ReceiptNumber
End If
' Display the Rent Payment dialog box
If Editor.ShowDialog() = DialogResult.OK Then
' If the user had clicked OK,
' Prepare the elements of the XML file
ReceiptNumber = CInt(Editor.txtReceiptNumber.Text)
DateReceived = Editor.dtpDateReceived.Value
AllocationCode = Editor.txtAllocationCode.Text
TenantAccountNumber = Editor.txtTenantAcntNber.Text
TenantName = Editor.txtTenantName.Text
PropertyCode = Editor.txtPropertyCode.Text
PropertyType = Editor.txtPropertyType.Text
PaymentFor = Editor.cbxMonths.Text & " " & Editor.txtYear.Text
AmountReceived = CDbl(Editor.txtAmountReceived.Text)
' Get a reference to the root element
Dim RootElement As XmlElement = DOMPayment.DocumentElement
' Create an element named Payment
Dim PaymentElement As XmlElement = DOMPayment.CreateElement("Payment")
' Add the new element as the last node of the root element
RootElement.AppendChild(PaymentElement)
' Get a reference to the root element
RootElement = DOMPayment.DocumentElement
' Create an element
PaymentElement = DOMPayment.CreateElement("ReceiptNumber")
' Create the value of the new element
Dim TextPayment As XmlText = DOMPayment.CreateTextNode(ReceiptNumber)
' Add the new element as a child of the Payment element
RootElement.LastChild.AppendChild(PaymentElement)
' Specify the value of the new element
RootElement.LastChild.LastChild.AppendChild(TextPayment)
' Follow the same logic for the other elements
PaymentElement = DOMPayment.CreateElement("DateReceived")
TextPayment = DOMPayment.CreateTextNode(DateReceived.ToString("d"))
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextPayment)
PaymentElement = DOMPayment.CreateElement("AllocationCode")
Dim TextAllocation As XmlText = DOMPayment.CreateTextNode(AllocationCode)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("TenantAccountNumber")
TextAllocation = DOMPayment.CreateTextNode(TenantAccountNumber)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("TenantName")
TextAllocation = DOMPayment.CreateTextNode(TenantName)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("PropertyCode")
TextAllocation = DOMPayment.CreateTextNode(PropertyCode)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("PropertyType")
TextAllocation = DOMPayment.CreateTextNode(PropertyType)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("PaymentFor")
TextAllocation = DOMPayment.CreateTextNode(PaymentFor)
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
PaymentElement = DOMPayment.CreateElement("AmountReceived")
TextAllocation = DOMPayment.CreateTextNode(FormatNumber(AmountReceived))
RootElement.LastChild.AppendChild(PaymentElement)
RootElement.LastChild.LastChild.AppendChild(TextAllocation)
DOMPayment.Save(Filename)
ShowRentPayments()
End If
End Sub
|
Private Sub btnClose_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click
Close()
End Sub
|
| Allocation 1 | Allocation 2 | Allocation 3 | Allocation 4 | |
| Allocation Code | 4205-8274 | 5920-9417 | 2792-4075 | 7957-7294 |
| Date Allocated | 8/12/2002 | 2/18/2004 | 3/24/2004 | 10/26/2007 |
| Account # | 24-68-84 | 57-97-15 | 19-38-84 | 20-48-46 |
| Prop Code | 726-454 | 625-936 | 371-801 | 727-768 |
| Contract Length | 12 Months | 3 Months | 12 Months | 6 Months |
| Start Date | 10/1/2002 | 4/1/2004 | 6/1/2004 | 2/1/2008 |
![]() |
![]() |
| Date Received | Allocation Code | Payment For | Amount | ||
| Month | Year | ||||
| Payment 1 | 10/25/2002 | 4205-8274 | October | 2002 | 1150.50 |
| Payment 2 | 11/28/2002 | 4205-8274 | November | 2002 | 1150.50 |
| Payment 3 | 4/28/2004 | 5920-9417 | April | 2004 | 1750.00 |
| Payment 4 | 7/5/2004 | 2792-4075 | June | 2004 | 1250.25 |
| Payment 5 | 6/3/2004 | 5920-9417 | May | 2004 | 1750.00 |
| Payment 6 | 7/30/2004 | 2792-4075 | July | 2004 | 1250.25 |
| Payment 7 | 7/5/2004 | 5920-9417 | June | 2004 | 1750.00 |
| Payment 8 | 12/30/2002 | 4205-8274 | December | 2002 | 1150.50 |
![]() |
![]() |
|
|
|
||
| Previous | Copyright © 2008, Yevol | Next |
|
|
||