Search code examples
xmlvb.netreadxmlxmldataset

How to load the last XML file and put in a dataset vb.net


I am stuck again. Please help if any of you can. I am really appreciate it.

I am creating XML files and load them back again. I use this following code to write xml to a folder. the code below will put date and time to the file name. and this code works fine.

    Dim filename As String = Server.MapPath("XML\" & SESSIONid & "_" & Replace(timenow, ":", "-") & ".xml")
    dSetPupil.WriteXml(filename, True)

Again, I want to load the last xml file back and put in a dataset. I normally write code like

    Dim dSet as new DataSet = ReadXml(Server.MapPath("AAA.xml")

But how can i find the last xml file and read it ?

Thanks xo much. Hope you guys having a nice day.


Solution

  • Dim strLastXmlFileWritten As String = String.Empty
    
    Dim lstFiles As List(Of IO.FileInfo) = New IO.DirectoryInfo(Server.MapPath("XML\")).GetFiles().ToList()
    
    Dim dteCreated As Date = DateTime.MinValue
    
    For Each objFile As IO.FileInfo In lstFiles
    
        If objFile.CreationTime > dteCreated AndAlso _
           objFile.Extension = ".xml" Then
    
            dteCreated = objFile.CreationTime
            strLastXmlFileWritten = objFile.FullName
    
        End If
    
    Next