Need some help reading from XML

  • Thread starter Thread starter Hendri Bissolati
  • Start date Start date
H

Hendri Bissolati

Guest
Hi there.

I am using the code below for writing to .xml and then reading the data from the .xml again. The reading part is where I am having some difficulty and I have no clue as to what I am doing Wrong.

The problem I am having is the data is being desplayed all in the PhoneNumberTextbox like this:

<Client> <FullName>Bettie Louw</FullName> <E-Mail>E-Mail</E-Mail> <PhoneNumber>074 729 2631</PhoneNumber> <Adress>Kraaifontyn</Adress> </Client>

All in one row instead of only the phone number itself in the PhoneNumberTextbox and the same for the E-Mail and Adress.

Dim ClientDataFolder As String = "F:\OuPa Piet cc\Detergent Sales & Client Details Data\Client Details\"

Sub CreateXML()
If System.IO.File.Exists(ClientDataFolder & FullNameTextBox.Text & ".xml") Then
System.Windows.Forms.MessageBox.Show("File allready exist.")
Else
MsgBox("File do not exist and will be created.")
Dim settings As New XmlWriterSettings()
settings.Indent = True
Initialize Xmlwriter
Dim XmlWrt As XmlWriter = XmlWriter.Create(ClientDataFolder & FullNameTextBox.Text & ".xml", settings)
Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\Users\Hendri\Desktop\MyName.xml", settings)
With XmlWrt
Write the Xml Declaration.
.WriteStartDocument()
Write a comment.
.WriteComment("XML DataBase.")
Write the root element.
.WriteStartElement("Data")

Start first client.
.WriteStartElement("Client")

The Client Nodes.
.WriteStartElement("FullName")
.WriteString(FullNameTextBox.Text.ToString())
.WriteEndElement()

.WriteStartElement("PhoneNumber")
.WriteString(PhoneNumberTextBox.Text.ToString())
.WriteEndElement()

.WriteStartElement("E-Mail")
.WriteString(EmailTextBox.Text.ToString())
.WriteEndElement()

.WriteStartElement("Adress")
.WriteString(AdressTextBox.Text.ToString())
.WriteEndElement()

The end of this client.
.WriteEndElement()

Close the XML Text Writer.
.WriteEndDocument()
.Close()
End With
MsgBox("Xml File Saved.")
End If
End Sub

Sub ReadFromXML()
If (FullNameTextBox.Text = "") Then
MsgBox("No file name enterd")
Else
If (System.IO.File.Exists(ClientDataFolder & FullNameTextBox.Text & ".xml")) Then
If System.IO.File.Exists(ClientDataFolder & NamesListBox.Text & ".xml") Then
Dim document As XmlReader = New XmlTextReader(ClientDataFolder & FullNameTextBox.Text & ".xml")
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name = "PhoneNumber") Then
PhoneNumberTextBox.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "E-Mail") Then
EmailTextBox.Text = document.ReadInnerXml.ToString()
End If
If (document.Name = "Adress") Then
AdressTextBox.Text = document.ReadInnerXml.ToString()
End If
End If
End While
document.Close()
Else
MsgBox("The file name you selected was not found.")
End If
End If
End Sub



Please help me correct this and feel free to look at and comment on the writing part of the code for this is the first time I work with .xml.


Hendri Bissolati

Continue reading...
 
Back
Top