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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datasets to xml 1

Status
Not open for further replies.

SMURF75

Programmer
Aug 28, 2004
87
GB
Hi

I just cant get this to work. The idea is to add a user to an xmlfile. It works when when a new file is created. But when i want to add a dataset to an existing file it fails.



Code:
Try
       
            Dim dsSet As New DataSet("Users")
            Dim dtTables As New DataTable("User")
            Dim drRow As DataRow

            dtTables.Columns.Add("UserId", System.Type.GetType("System.Int32"))
            dtTables.Columns.Add("Password", System.Type.GetType("System.String"))
            dtTables.Columns.Add("FName", System.Type.GetType("System.String"))
            dtTables.Columns.Add("LName", System.Type.GetType("System.String"))

            drRow = dtTables.NewRow()
            drRow("UserId") = txtUserID.Text
            drRow("Password") = txtPassword.Text
            drRow("FName") = txtFName.Text
            drRow("LName") = txtLName.Text

            dtTables.Rows.Add(drRow)

            dsSet.Tables.Add(dtTables)

            saveDataSet(dsSet)

            Me.Close()

        Catch ex As Exception
            logError(ex)
        End Try

    End Sub



Public Function saveDataSet(ByVal ds As DataSet) As Boolean

        Try
            strPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(). _
            GetName().CodeBase)
            strPath = strPath & "\" & "users2.xml"

            Dim fs As New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)

            Dim xw As New XmlTextWriter(fs, System.Text.Encoding.UTF8)

            ds.WriteXml(xw,    System.Data.XmlWriteMode.WriteSchema)

            xw.Close()

        Catch ex As Exception
            Throw ex
        End Try

End Function


- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
First I would say, avoid using this folder:
Code:
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(). _
            GetName().CodeBase)

If the app is installed in the program files folder, there can and often enough will be security issues with writing to the app.

I would recommend:
Code:
'for user specific settings storage
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
'or for application specific settings storage
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

Also, you don't need to create the file stream or XML reader. you can use:
Code:
ds.WriteXml(strPath, System.Data.XmlWriteMode.WriteSchema)

also, you will likely want to read in the XML file before writing it, as writing it will overwrite it I beleive.

And what may be causing your issue, if the file is open, you will not be able to write to it.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I was starting to think that way.... open first then add then rewrite. Now I know.. thx! The other tips were also useful.

plaff.. a star on your forehead

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top