drag and drop folder containing XML files
-
Tuesday, September 30, 2008 3:30 AMGood Day!
I would like to know if it is possible to drag and drop multiple folders containing XML files into a datagrid and have the XML files be read into the datagrid.
currently, I am able to drag and drop multiple files and have them be loaded into the datagrid.
Anyone can help?
Thanks!
All Replies
-
Wednesday, October 01, 2008 9:19 AMModeratorHi Kooks,
If you use Dataset(or datatable).WriteXml to save the data into the XML file, then you can use the DataSet(or datatable).ReadXml to fetch the data into the datagridview control.
If XML file isn't generated in the above way, it is structue data. You can use the TreeView control to display this XML file.
Best regards,
Riquel -
Wednesday, October 01, 2008 5:09 PMHi Riquel,
I use the ReadXML. I don't have a problem reading the data.
What I am trying to do is drag and drop folders if that is possible.
The users of the application I have developed have about 300 or more XML files they need to load each time in about 10 seperate folders. I just want to know if I can make it easier on them by giving them the ability to drag and drop the folders and have the files in the folders automatically loaded.
Thanks!
Kareem -
Wednesday, October 01, 2008 6:45 PM
Hi Kareem, hope your well.
I actually saw your post a couple of days ago and I understand where you are coming from and I wanted to create a sample application to see if it possible. What I have done is I opened Notepad to see if I could drop a folder on it and it looked like it worked. Didn't do anything but the drag and drop icon changed to suggest it's possible.
What you could do, as it wouldn't be a big change, is to code up a method that scans a directory and reads in the XML files (it would need to check it was reading in the correct ones) and then drive that method from a user selecting a directory using a folder browser dialog or something similar.
If it is possible to drop folders onto your application then that method (function/sub) would be used. Even if it isn't possible then you have the ability to read in a directory of files anyway.
-
Thursday, October 02, 2008 12:45 AM
Hi Derek!
Thank you for your reply.
I tried what you said and it worked, however, being a newbie and a self-learner, I believe my code is quite crude and would probably give any decent programmer a heart attack!!
I know my computer felt like it almost had a heart attack running it!!!
I am adding it here without shame and hope that maybe you can help me clean it up a bit and make it more efficient.
I tested it with a directory containing about 300 XML files in 10 folders and my computer went nuts and i had to stop it. I tried it again with folder containing 30 XML files in 1 folder and it worked. So I know it is operational, just very slow and a processor killer.
Can you help?
Code SnippetImports
System.IOImports
System.TextPublic
Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click'----- Determine the name and path of the directories, which are located in a specific directory and list them in textbox1
For Each y As String In My.Computer.FileSystem.GetDirectories("C:\Users\Kareem\Desktop\new\")
TextBox1.Text += y & vbCrLf
'----- Determine the name and path of each file located in each path in Textbox1 and list them in Textbox2
For Each x As String In My.Computer.FileSystem.GetFiles(y)
TextBox2.Text += x.ToString & vbCrLf
'----- Extract file name and path from textbox2
For Each s As String In TextBox2.Lines
'----- Read XML file
If s.ToString <> Nothing Then
Dim sr As New StreamReader(s, Encoding.ASCII)'---- Display data in Datagrid
DataSet1.ReadXml(s)
DataGridView1.DataSource = DataSet1
DataGridView1.DataMember =
"row" End If Next Next Next
End SubEnd
Class -
Thursday, October 02, 2008 3:50 AMModerator
Hi Kareem,
Put For Each s As String In TextBox2.Lines out the above Loop. I think that you execute some repeated operation in this scenario. Second I don't see that you ues the Drag and Drop feature here. Try the following code snippet. If you have any further issues, feel free to tell us.
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Public Sub New()
InitializeComponent()
AddHandler Me.MouseMove, AddressOf Form1_MouseMove
Me.AllowDrop = True
AddHandler Me.DragDrop, AddressOf Form1_DragDrop
AddHandler Me.DragEnter, AddressOf Form1_DragEnter
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
Try
MessageBox.Show(files(0))
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
End Sub
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
System.Console.WriteLine(e.X.ToString() + "," + e.Y.ToString() + "" & Chr(10) & "")
End Sub
End Class
Best regards,
Riquel -
Thursday, October 02, 2008 9:02 PM
Hi mate,
Reading in a lot of files will always take a bit of time but I can see a bit of a problem, too many strings. Strings in .NET are immutable which means when you create a string it never changes. When you append one string to another string a third string is created. So when you have a loop of 300 where in the loop you append a string to another string you end up creating 301 strings! Ouch!
You have here three nested loops and each loop does some string appending (concatination).
I've had a rough day so no code improvements but with the above in mind see if your able to improve your code. post it again and I'll review it again no worries.
Riquel has posted the drag and drop code so one thing you want to do is take the code above, that is behind the button click event and make it a sub procedure. Pass a string array to that sub procedure, the directories to read the files from, and then you can use Riquels code to call that sub procedure.
-
Thursday, October 02, 2008 9:28 PM
Kooks,
I was going to suggest you use listbox instead of textbox. Lists seemed to be more efficient. I recently created a little application to read close to 1000 files in many directories and convert them from word docs to plain text files. I had the listboxes populate the files and status of what was done with the files. Some were removed while others were converted so i wanted to keep track of the progress. But it only took a few minutes to convert all the files. Not sure this will help your situation but lists of some sort might be better. And keep in mind that you don't need to use any controls to work with the filenames. if you want to see them ok but you can work with arrays and lists in the background.
Hope this helps
Jeff

