Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I read/write to XML file

Status
Not open for further replies.

ityles

Programmer
Apr 15, 2002
6
US
I need to read in an XML file, traverse to the node position that I want to insert/update, and then save the changed doc.

Using the MSXML2 object I can load the file fine, but can't find any helpful examples of how to create a new node with all the attributes and text that you want in it, or how to get to the spot you want etc.

Is the MSXML the right tool to use here? I didn't think that the XMLTextReader and XMLTextWriter were appropriate because they are ReadOnly. I thought this would be fairly sraight-forward operation but can't find any VB.NET samples that do this.

Please help!
Thanks,
I.
 
You must parse the XML file into a dataset and then add a new row to that dataset, then write it back out to XML...
Like this:

Public Class Users

Private usersDataSet As DataSet
Private usersDataTable As DataTable
Private applicationRootDirectory as String
Private usersInfoFile as String

Sub New(ByVal applicationRootDirectory As String)
Me.applicationRootDirectory = applicationRootDirectory
usersInfoFile = applicationRootDirectory & "\UsersInfo.xml"
usersDataSet = New DataSet()
usersDataSet.ReadXml(usersInfoFile)
usersDataTable = usersDataSet.Tables(0)
End Sub

Public Function addUser(ByVal user As User) As Boolean
Dim row As DataRow
For Each row In usersDataTable.Rows
If UCase(row("username")) = UCase(user.getUserName()) Then
Return False
End If
Next
row = usersDataTable.NewRow()
row("username") = user.getUserName()
row("firstname") = user.getFirstName()
row("lastname") = user.getLastName()
row("password") = user.getPassword()
row("encryption") = user.getEncryption()
row("directory") = user.getDirectory()
row("admin") = user.getAdmin()
usersDataTable.Rows.Add(row)
usersDataSet.WriteXml(usersInfoFile)
Directory.CreateDirectory(applicationRootDirectory & user.getDirectory())
Return True
End Function
End Class


Cheers! Life is like a box of chocolates, sweet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top