How to read and write Existing XML file Using VB.NET
------------------------------------------------------------------------------------------------Imports System.Data.DataSet
Public Class SettingsXML
Dim XML_FILE_PATH As String = "C:\XMLData.xml"
Dim xmldataset As New Data.DataSet
Sub WriteXML(ByVal ServerIP As String, ByVal Name As String, ByVal Age As String, ByVal SignaturePath As String, ByVal NotifyTimer As Integer, ByVal Timer As Integer)
again:
Try
xmldataset.ReadXml(XML_FILE_PATH)
xmldataset.Tables(0).Rows(0).Item("ServerIP") = ServerIP
xmldataset.Tables(0).Rows(0).Item("Name") = Name
xmldataset.Tables(0).Rows(0).Item("Age") = Age
xmldataset.Tables(0).Rows(0).Item("SignaturePath") = SignaturePath
xmldataset.Tables(0).Rows(0).Item("NotifyTimer") = NotifyTimer
xmldataset.Tables(0).Rows(0).Item("Timer") = Timer
xmldataset.AcceptChanges()
xmldataset.WriteXml(XML_FILE_PATH)
xmldataset.Dispose()
Catch ex As Exception
'MsgBox("XML Error" & ex.ToString)
Dim xmlfile As New CreateXMLFile()
xmlfile.WRITEXML()
GoTo again
End Try
End Sub
Function ReadXML(ByVal a As Integer) As String
again:
Try
xmldataset.ReadXml(XML_FILE_PATH)
If a = 0 Then
Return xmldataset.Tables(0).Rows(0).Item("ServerIP")
End If
If a = 1 Then
Return xmldataset.Tables(0).Rows(0).Item("Name")
End If
If a = 2 Then
Return xmldataset.Tables(0).Rows(0).Item("Age")
End If
If a = 3 Then
Return xmldataset.Tables(0).Rows(0).Item("SignaturePath")
End If
If a = 4 Then
Return xmldataset.Tables(0).Rows(0).Item("NotifyTimer")
End If
If a = 5 Then
Return xmldataset.Tables(0).Rows(0).Item("Timer")
End If
Catch ex As Exception
Dim xmlfile As New CreateXMLFile()
xmlfile.WRITEXML()
GoTo again
End Try
End Function
End Class
--------------------------------------------------------------------------------------------------
How to write a New XML File Using VB.NET
Public Class CreateXMLFile
Sub WRITEXML()
Dim mywriter As System.Xml.XmlTextWriter = New System.Xml.XmlTextWriter("C:\XMLData.xml", Nothing)
With mywriter
.Indentation = 4
.IndentChar = " "
.Formatting = .Indentation
.WriteStartDocument()
.WriteComment("Settings for the Connection")
.WriteStartElement("settings")
.WriteElementString("ServerIP", "192.168.1.42")
.WriteElementString("Name", "name")
.WriteElementString("Age", "25")
.WriteElementString("SignaturePath", "D:/sign")
.WriteElementString("NotifyTimer", "1")
.WriteElementString("Timer", "1")
.WriteEndElement() 'For settings
.WriteEndDocument()
End With
mywriter.Flush()
mywriter.Close()
End Sub
End Class