Search code examples
xmlvb.netdatatabledatasetwritexml

How to have dynamic file name when using dataset.writexml vb.net


... wish all of you are having a nice Valentine's Day .. For me, I am stack at work finishing my program.

I have a piece of code to write an xml file.

    Dim ds As New DataSet
    Dim dt As DataTable = GetDtable()

    ds.Tables.Add(dt)

    Dim filename As String = Server.MapPath("XmlDoc.xml")

    Dim writer As New System.IO.StringWriter
    dt.WriteXml(filename, True)

That work will write an XML file whatever I put in the datatable (or dataset). It is just a simple code and it works fine.

The problem is it will replace the old file everytime the new file is created. I really don't want that to happen.

I also want to include 2 variables (username, sessionID) also time of the file created in the file name.

So file name will be Username_sessionID_time.xml rathen than a fixed name.

Can anyone tell me how can i do that ?

thanksssssssss so much.


Solution

  • Assuming Username and SessionID are strings with no illegal file name characters, you can simply concatenate them when you define the value for the filename variable...

    Dim filename As String = Server.MapPath(username & "_" & sessionID & "_" & Replace(DateTime.Now.ToString(), "/", "-") & ".xml")